Home > other >  Redux Saga & Redux Toolkit watcher problem
Redux Saga & Redux Toolkit watcher problem

Time:11-09

hope you have a nice day! I am newbie to redux and redux saga and i wanted to ask you all help.

This is the sandbox https://codesandbox.io/s/staging-wave-3ewyr?file=/src/store.js

The problem is that when i click on increment button the value of "counter" inside the store become 0 then it will be incremented by 1. Clicking on Decremenet and Sum works because they are not using sagas. Example: Click on decrement: counter become -1 Click on increment: counter become 0 then 1 (it should be 0 cause we expect to be: -1 1) Anyone can explain me where is the problem?

Thank you all!

CodePudding user response:

The problem is in the final part of your reducer:

default:
  return 0;

Since there is no case for the INCREMENT action, the code execution goes to default and resets the counter to zero.

The solution is to return the current state instead:

default:
  return state;
  • Related