- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为什么 React 中没有异步 getState 函数?
文档告诉我们 setState 是异步的。很好,但这意味着我们无法安全地使用 this.state,并且我们还需要一个异步 getState 来尊重执行顺序。
据我了解,我们永远不应该使用 this.state 并使用这样的 getState 函数:
getState(callback) {
this.setState((prevState) => {
callback(prevState) ;
});
}
...
this.getState((curState) => {
// we now can use the current state safely
}
我的思维方式有什么遗漏的吗?为什么 React 中不存在这样的函数?
--编辑--
正如我的一个 friend 告诉我的那样,这还不清楚,因为我不相信第一个答案,所以让我们分析一些代码:
simpleFunc() {
setState({ "counter" : 1 });
setState({ "counter" : 2 });
this.state.counter // => no garanty about the value
getState((curState) => { // ensure curState.counter is 2 });
}
这个简单的示例表明,我们不能在所有情况下直接使用 this.state,因为 setState 是异步的。
这是一个可以使用 getState 的反例: http://codepen.io/Epithor/pen/ZLavWR?editors=0010#0
简短回答:不好的做法,即使不确定 getState 是否给我们当前的
解决方法很简单,但我们可以分解一些函数并使用它们而不关心上下文,这一事实似乎很有趣,不是吗?
因此,当许多事件以特定顺序发生时,有些事件会更改状态,有些事件会读取状态:当事件使用 this.state 读取状态时,您如何确定良好的状态,因为所有更改都是异步的?
事实上一切都与时间有关:
T : event 1, change state
T+1ms : event 2, change state
T+2ms : event 3, read state
T+3ms : event 4, change state
由于您无法预测事件 1 或 2 的 setState 究竟何时发生,如何保证事件 3 真正读取事件 2 中设置的状态?
简短回答:事件在 JS 堆栈中排队,而状态更改在内部 React 队列中排队。在交手之前,内部 React 队列已完全出栈。
最佳答案
一般来说,您绝对可以直接使用this.state
。您永远不应该直接改变状态(this.state.foo = 0
),而是在想要改变状态时使用setState
。
通常 setState
看起来像这样:
this.setState({
foo: 0
})
然后您就可以在 render()
函数中安全地使用 this.state.foo
。
但是需要注意的是,由于 setState
的异步特性,您无法保证在 之后能够立即访问
已被调用。this.state
setState
myFunc(baz) {
this.setState({
foo: baz + 1
})
console.log(this.state.foo) // not guaranteed
}
做得更好
myFunc(baz) {
const bazOne = baz + 1
this.setState({
foo: bazOne
})
console.log(bazOne)
}
或者使用setState
函数的第二个参数,用作setState操作完成时执行的回调。在该回调中,您将可以访问更新后的状态,即 this.state
:
myFunc(baz) {
this.setState({ foo: baz + 1 }, () => {
console.log(this.state.foo) // guaranteed in callback
});
}
参见:https://facebook.github.io/react/docs/react-component.html#setstate
关于reactjs - React setState/getState 和异步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41896878/
我一直在尝试将 Redux 集成到项目中。 我按照使用示例进行操作,但收到错误store.getState is not a function。 所以我知道其他人也问过类似的问题,但情况略有不同。 R
我正在尝试将 react 粘性标题添加到我的步进器中。 但问题是,如果我在 App.js 中添加它,它就不会渲染。 所以我开始调试 App.js 代码。 如果我在 App.js 的渲染方法中提供控制台
我是使用线程的新手,我正在尝试找出一种方法来判断线程是否终止,以及从线程收集一些信息。但是,每当我尝试调用其中一个线程的方法(包括 thread.getState())时,我都会收到空指针异常。请我想
我有这个字节码: new java.lang.Object // stack is [newObjectRef] dup // Stack is [newObjectR
我对 redux 中的 getState() 有点困惑。我正在使用 thunk 中间件。 我有一个身份验证操作,它是一个异步操作。但是我有一个在其之前运行的操作,它检查状态中是否存在 token 以及
很难说出这里问的是什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或言辞激烈,无法以目前的形式合理回答。如需帮助澄清此问题以便可以重新打开,visit the help center . 8年前关闭
我正在构建一个同构的 React 应用程序,并且当用户发送请求时执行以下任务: 1) 假设用户点击 /about-us,react-routers matchPath 找到合适的组件并返回它。 2)
我有一个 React 应用程序,现在我使用 redux。该应用程序运行正常,但现在我收到错误: Uncaught TypeError: Cannot read property 'getState'
我想使用 SlidingPaneLayout.getState();但没有找到。是否被删除以及何时删除? public void showAudioPlayer() { mActivi
我如何从 zfcUser 获取状态 在 view/index.phtml 中,我从 $this->zfcUserIdentity()->getState(); 获取它; 但现在我需要在其他模块/ Co
我在 React Native 中的商店遇到了一些问题。我在我的文件 store/index.js 中配置了存储。我将其导出,然后导入到 App.js 中。来自 reducer 的 Action 正确
我想断言,当函数使用 store.getState() 获取我的 redux 状态值时,它会根据该状态的条件执行各种操作。对于使用 store.getState() 方法的某些测试,我如何断言/模拟我
让我坚持了好几天的问题是,虽然我的 redux devtool 显示状态更新成功,没有任何类型的突变,并且 View 组件重新渲染成功,但是当我调用 getState() 时,它总是返回初始状态并且不
我有一个下面的 Singleton 类,在我的 getStatement 方法中,我通过执行 if 检查来填充 CHM。 public class CacheHolder { private st
我正在尝试获取 webRTC 应用程序的统计信息以测量音频/视频流带宽。我检查了这个 question我发现它非常有用;但是,当我尝试使用它时,我得到了 TypeError: Not enough a
func (t *ballot) initBallot(stub shim.ChaincodeStubInterface, args []string) peer.Response { if
我正在尝试使用 redux 制作一个 nextjs 应用程序,并且我正在设置基本框架。 它如下所示。 服务器.js const express = require("express"); const
我昨天刚开始使用 redux,在阅读了不同的库之后,我决定使用 RTK 的切片路由。 对于我的异步,我决定使用 RTK 查询而不是使用 createAsyncThunk,并且我对从另一个切片访问状态的
我正在尝试使用 WebRTC 的 api 中的 getStat() 来查看它是否提供了任何有用的信息测量延迟和其他视频流数据。问题是关于如何使用它的信息并不多。 即使是较旧的现有示例也非常罕见,但此后
假设我在全局商店中有很多实体 苹果 商店 果汁 如果我想要函数 getAppleJuicePrice,我可以通过 2-3 种方式实现 通过参数 function getAppleJuicePrice(
我是一名优秀的程序员,十分优秀!