[英]How to get updated state in useEffect when using update loop
So as you probably know, in normal mode, we use update dependencies to get notice when the state updated, like this:因此,您可能知道,在正常模式下,我们使用更新依赖项来获取状态更新时的通知,如下所示:
const [val, setVal] = useState();
useEffect(() => {}, [val]]);
But in my case, I have an array in my state and I'm trying to update it in a loop in my useEffect like this:但就我而言,我的状态中有一个数组,我试图在我的 useEffect 中循环更新它,如下所示:
const [val, setVal ] = useState([...]);
useEffect(() => {
anotherArr.forEach(i => {
// get val and modify some indexes
setVal(modifiedValuesArray);
}
}, []);
In this case, every time forEach loop runs, I'm getting the initial val (I know because val is not a dependency of useEffect) but if I put it as a dependency, it will update twice.在这种情况下,每次 forEach 循环运行时,我都会得到初始 val(我知道因为 val 不是 useEffect 的依赖项)但如果我将其作为依赖项,它将更新两次。 what is the solution for this?
解决这个问题的方法是什么?
EDIT: Basically, I mean when I update state in a round of loop in useEffect, on the next round, I'm not getting the updated state but the initial state before entering the loop.编辑:基本上,我的意思是当我在 useEffect 的一轮循环中更新状态时,在下一轮中,我没有获得更新的状态,而是在进入循环之前的初始状态。 And I know, that is because of the nature of useEffect which gives us a memorized value of state (since we didn't pass it as a dependency to avoid the additional execution), but what is the solution in these types of scenarios.
而且我知道,这是因为 useEffect 的性质给了我们一个状态的记忆值(因为我们没有将它作为依赖项传递以避免额外执行),但是在这些类型的场景中的解决方案是什么。
Use setVal
only once, not during an forEach
loop. setVal
只使用一次,而不是在forEach
循环中使用。 setState
is async
so you can't depend on it like its synchronous. setState
是async
所以你不能像它的同步那样依赖它。 In your example setVal
will actually be executed some time in the future.. Do you maybe have a codesandbox example?在您的示例中,
setVal
将在将来的某个时间实际执行.. 您是否有一个代码和框示例?
EDIT: You don't get updated state on "next round".编辑:您不会在“下一轮”中获得更新的状态。
setState
will be executed N times, and will put it in an update queue, and React will probably only update the last setState
value for optimisation. setState
将执行 N 次,并将其放入更新队列中,而 React 可能只会更新最后一个setState
值以进行优化。 Also, your example useEffect
will run only once..此外,您的示例
useEffect
将只运行一次..
I came across this answer:( https://stackoverflow.com/a/59422750/2728431 ) and it solved my problem.我遇到了这个答案:( https://stackoverflow.com/a/59422750/2728431 )它解决了我的问题。
for getting updated state, (as @usafder said in a comment), we need to pass state as a value in an arrow function just like this:为了获得更新的状态,(正如@usafder 在评论中所说),我们需要像这样在箭头函数中将状态作为值传递:
const [val, setVal ] = useState([...]);
useEffect(() => {
anotherArr.forEach(i => {
setVal(val => {
// modify based on provided val on arrow function
return modifiedValuesArray
});
}
}, []);
Whenever you need to update the state using its current value, you need to send in a function instead to the state setter which would give you the updated current value of the state as a param.每当您需要使用其当前值更新状态时,您都需要将一个函数发送给状态设置器,该函数将为您提供状态的更新当前值作为参数。 So in your case it would be something like below:
因此,在您的情况下,它将类似于以下内容:
const [val, setVal ] = useState([...]);
useEffect(() => {
anotherArr.forEach(i => {
setVal((currVal) => {
let modifiedValuesArray = [];
// your update logic here (use currVal instead of val)
return modifiedValuesArray;
});
});
}, []);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.