React setState not updating state

前端 未结 9 643
慢半拍i
慢半拍i 2020-11-22 05:41

So I have this:

let total = newDealersDeckTotal.reduce(function(a, b) {
  return a + b;
},
0);

console.log(total, \'t         


        
9条回答
  •  一向
    一向 (楼主)
    2020-11-22 05:58

    I had an issue when setting react state multiple times (it always used default state). Following this react/github issue worked for me

    const [state, setState] = useState({
      foo: "abc",
      bar: 123
    });
    
    // Do this!
    setState(prevState => {
      return {
        ...prevState,
        foo: "def"
      };
    });
    setState(prevState => {
      return {
        ...prevState,
        bar: 456
      };
    });
    

提交回复
热议问题