简体   繁体   English

使用更新循环时如何在useEffect中获取更新状态

[英]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. setStateasync所以你不能像它的同步那样依赖它。 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.

相关问题 如何在 React useEffect 挂钩中获取更新的 state 值 - How to get updated state value in React useEffect hook 如何在 Redux 状态更新时更新功能组件? - How to get a functional component to update when Redux state gets updated? React - 如何在 useEffect 中更新 state 而不会导致无限循环? - React - How to update the state in useEffect without causing an infinite loop? 无法获取更新的 state 值 useState 和 UseEffect - Cannot get the updated the state value useState and UseEffect 如何在 React 的 useEffect 中将 State 更新传递给自定义渲染循环? - How To Pass State Update To Custom Render Loop In React's `useEffect`? 如何更新 state 但不触发无限 useEffect 循环? - How do I update state but not trigger the infinite useEffect loop? State 不使用 UseEffect 更新 - State does not update using UseEffect React挂钩:使用useEffect时无法获取状态更新 - React hooks: Not getting the state update when using useEffect 在 useEffect 依赖数组中使用 Redux state 时如何避免无限循环? - How do I avoid infinite loop when using Redux state in useEffect dependency array? State 来自 react useState 在使用 key 属性时更新,但需要 useEffect 或类似方法才能更新 - State from react useState is updated when key property is used but requires useEffect or similar method to update otherwise
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM