Search code examples
reactjsreact-state-managementreact-easy-state

How to change more than 1 state in React-easy-state?


I am not able to mutate more than 1 state through one function using React-easy-state

I have used batch in my following example and also used mutation separately. However, the code somehow disregards my 2nd state.

piece of the code looks like this :

batch(() => {
  // batch is used to test out whether it will trigger both store changes.
  store.counter = store.counter + 1;
  store.sumNum = 10; // this is never updated/mutated.
});

for reproduction link to sandbox.


Solution

  • You don't have any state called sumNum.

    // your store
    
    const myStore = store({
      rndNum: "",
      counter: 1,
      someNum: 2, <--- not sumNum
      showRes: false 
    });
    

    Change it to someNum and it works.