Side Effects after the execution reducer'a?

Hello, I study Redux. As I understand it any asynchronous actions and side effects must occur in midleware. But If you say you have changed state in redusere and I want it to save in database ( or some part of it, not the point). What then? As I do not understand the logic. Why reduser completes the chain changes state? Please explain. We can use the localStorage. Thank you.
July 9th 19 at 13:39
2 answers
July 9th 19 at 13:41
Solution
Reducer not completes the chain changes state. From receive action and returns a new state. In fact, only reducer you can "modify" state.
Saving data in localStorage is also a side effect, and it must occur in midleware.

For example, redux-thunk:
const asyncAction = () => {
 return (dispatch, getState) => {
 localStorage.setItem('state', JSON.stringify(getState()))
 return dispatch(someAction())
}
}
Yes, it is clear, and that if necessary, state to maintain, after the change in redusere? because mallware can only be up to him to deliver while state has not changed. - Coby commented on July 9th 19 at 13:44
You can subscribe to change of state store.subscribe(handleChange), after reducer change state is called handleChange().

You might need it https://github.com/michaelcontento/redux-storage
https://github.com/elgerlambert/redux-localstorage - Marlee.Bechtela commented on July 9th 19 at 13:47
: thank you, subscribe what you need) - Coby commented on July 9th 19 at 13:50
July 9th 19 at 13:43
As I understand it any asynchronous actions and side effects must occur in midleware.

I do not quite agree. In the action creators, too, you can almost with impunity to turn any side-effect (save the token in localStorage, for example, after LOGIN_SUCCESS).

Find more questions by tags FluxReduxReact