- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在努力理解确切的时间和原因 useReducer
与 useState
相比具有优势.有很多争论,但对我来说,没有一个真正有意义,在这篇文章中,我试图将它们应用到一个简单的例子中。
也许我遗漏了什么,但我真的不明白为什么 useReducer
应该在 useState
上的任何地方使用.我希望你能帮助我澄清这一点。
让我们以这个例子为例:
版本 A - 使用 useState
function CounterControls(props) {
return (
<>
<button onClick={props.increment}>increment</button>
<button onClick={props.decrement}>decrement</button>
</>
);
}
export default function App() {
const [complexState, setComplexState] = useState({ nested: { deeply: 1 } });
function increment() {
setComplexState(state => {
// do very complex logic here that depends on previous complexState
state.nested.deeply += 1;
return { ...state };
});
}
function decrement() {
setComplexState(state => {
// do very complex logic here that depends on previous complexState
state.nested.deeply -= 1;
return { ...state };
});
}
return (
<div>
<h1>{complexState.nested.deeply}</h1>
<CounterControls increment={increment} decrement={decrement} />
</div>
);
}
看到这个
stackblitz
import React from "react";
import { useReducer } from "react";
function CounterControls(props) {
return (
<>
<button onClick={() => props.dispatch({ type: "increment" })}>
increment
</button>
<button onClick={() => props.dispatch({ type: "decrement" })}>
decrement
</button>
</>
);
}
export default function App() {
const [complexState, dispatch] = useReducer(reducer, {
nested: { deeply: 1 }
});
function reducer(state, action) {
switch (action.type) {
case "increment":
state.nested.deeply += 1;
return { ...state };
case "decrement":
state.nested.deeply -= 1;
return { ...state };
default:
throw new Error();
}
}
return (
<div>
<h1>{complexState.nested.deeply}</h1>
<CounterControls dispatch={dispatch} />
</div>
);
}
看到这个
stackblitz
complexState
真的很复杂,我们有很多修改 Action ,每个 Action 都有很多逻辑。怎么样
useReducer
这里有帮助吗?对于复杂的状态,拥有单独的功能而不是拥有单个 200 行的 reducer 功能不是更好吗?
setState(oldstate => {...})
潜在的其他优势:
useReducer
真的很出色,并且不能轻易重写为带有
useState
的版本?
最佳答案
我相信这可能会以意见分歧而告终。然而,从一篇简单的文章中提取的内容对我来说很重要,所以在这里它在底部有一个指向整篇文章的链接。
useReducer() 是 useState() 的替代方法,它使您可以更好地控制状态管理并使测试更容易。所有的情况都可以用 useState() 方法来完成,所以总而言之,使用你觉得舒服的方法,对你和同事来说更容易理解。
引用文章:https://dev.to/spukas/3-reasons-to-usereducer-over-usestate-43ad#:~:text=useReducer()%20is%20an%20alternative,understand%20for%20you%20and%20colleagues .
关于reactjs - useReducer 与 useState 相比实际上有什么优势?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66386901/
这个问题在这里已经有了答案: 11年前关闭。 Possible Duplicate: Haskell: difference between . (dot) and $ (dollar sign) 好
我对 Java 平台没有任何了解,我想知道可以使用哪些工具(和方法)来帮助开发用 Java 编写的可维护代码。 我知道可以使用: 适用于任何环境的敏捷方法 用于单元测试代码的 jUnit/jMock(
我们的产品需要支持 IE9,但我们一直假设 IE9 支持 IE10+ CSS 规则。 是否有一种巧妙的方法来获取在 IE10+ 中有效但在 IE9 中不受支持的所有 CSS 规则,目的是在静态代码分析
我需要为 MyString 类重载运算符 + 和 +=。 MyString.h class MyString { char* m_pStr; }; 主要
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在升级现有的旧代码以使用 VS 2019*,在代码中我有以下函数在返回行失败: int foo(const char *fn) const { ofstream out(fn,ios::b
我想使用 R2D3 pacakge 在 R 中,但我不确定这个包与 D3.js 库的关系。 R2D3 是否以任何方式限制 D3 的功能?我们可以将 R 中的所有 D3 功能和特性与 R2D3 一起使用
我正在使用 WPF 语音识别库,试图在桌面应用程序中使用它来替代菜单命令。 (我想专注于没有键盘的平板电脑体验)。它可以工作 - 有点,除了识别的准确性太差以至于无法使用。所以我试着听写到 Word。
我在学校参加数据库类(class)。老师给了我们一个简单的练习:考虑以下简单的模式: Table Book: Column title (primary key) Column gen
我正在尝试学习 MVVM 模式,特别是当 View 表示数据库表时该怎么做,但 View 有几个元素表示单个数据库字段。举个简单的例子: 假设我有一个 DateTime 类型的数据库字段(每个数据库字
我有两张 table 。表单有约 77000 行。日志约有 270 万行。 以下查询将在不到一秒的时间内返回“30198”: SELECT COUNT(DISTINCT logs.DOCID) FRO
当您在 Eviews 中进行回归时,您会得到一组这样的统计数据: 在 R 中有没有一种方法可以在一个列表中获得所有/大部分关于 R 回归的统计数据? 最佳答案 请参阅summary,它将为大多数回归对
如果我枚举 type XType int const ( X1 XType = iota X2 ... Xn ) var XTypeNames = []string{"x1", "x2
我正在试用 ranger R包加速做了很多randomForest计算。我正在检查我从中得到的预测,并注意到一些有趣的事情,因为所做的预测完全不正确。 以下是比较 randomForest 的可重现示
我发现 Clang 编译速度比 GCC 慢了四倍。知道是什么原因造成的吗? ebg@tsuki(250)$ time /usr/bin/cc -DHC4 -DSAFETY -DNOREDUCE -DN
我注意到在尝试以 JSON 格式发布表单数据时,以下内容不起作用: $.ajax({ type: "POST", url: url, data: JSON.string
我的代码库中有很多 #if DEBUG/#endif 语句,它们大多具有断言类型逻辑,我不敢在生产环境中运行这些逻辑。 [Conditional("DEBUG")] public void Check
所以我正在开发一个平方根计算器,但我不知道 while 循环是否比 do while 循环更适合。 double x, y = 1.0, newY, squareRoot; bool
我有两个列表,一个是所有语言,另一个是网站拥有的语言子集,我的想法是返回所有语言,但如果子集的元素对应于所有语言的列表,则更改 bool 值的属性. 语言的DTO: public class DTOL
以下控制台应用程序运行正常 - 我很惊讶它没有出错。 class DelegateExperiments { //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
我是一名优秀的程序员,十分优秀!