Redux
Conclusion: reducers in redux must be pure function
whole redux process
what’s pure function?
A pure function is a function where the return value is only determined by its input values, without observable side effects.
pure function will not modify its input values.
let’s see code block below
1 | // pure situation |
1 | // non-pure situation |
side effect
compare above two situations, only one difference is non-pure situation, the reducers changes the state and return it.In this situation, what would happend? result is the view not re-render event though you dispatch action.
so what cause that.
it’s time to dig out source code.
the source code tell us clearly why reducers are pure function and why you cant execute asynchronous code in reducers
One more question: why redux only judge two obj's address in computer memories instead of comparing every properties of theme?
Because deep compare is so expensive. so redux make a rule that you must return a new state obj for redux to judge and then re-render view. otherwise ,it will throw error when you return undefined , or dont re-render when you return the original state obj.
Reference: