Do you know the best practice for managing state?
22/12/2017 4:00 AM by
State management is complex and time-consuming.
The redux pattern helps resolve this issue.
- Bad example: maintaining state on individual components
- Good example: use the redux pattern
The 4 principles of the redux pattern:
- The store is acted upon using special functions called reducers [ https://redux.js.org/docs/basics/Reducers.html ].
- State is immutable and reducers are the only part of the application that can change state.
To perform operations that require external dependencies (such as communicating with a web server), we can implement side effects. These can use external dependencies but they cannot directly modify the store. They can invoke reducers to modify the store when the side effect is complete
redux-saga is a library that provides redux application side effects.
The advantages of using redux-saga are:
- Collects all asynchronous operations in one place, making the code clearer.
- Uses an ES6 feature called Generators to make asynchronous flows easy to read, write and test.
- Generators also let these asynchronous flows look like your standard synchronous code (kind of like async/await in C#). This solves “callback hell” [https://en.wiktionary.org/wiki/callback_hell ]
Do you feel this rule needs an update?