Reputation: 87
Is it possible in redux to execute similar function to update state as it is in react? I mean to pass callback function as a parameter to action creator, which will execute after state update and inside this callback I will have possibility to access updated state? I've been searching a lot of different posts about this, but without any success. Why is it so hard to execute callback after state update in redux? In react this is very simple and we can simply do e.g.
this.setState( { isLoading: false }, () => {
console.log(this.state.isLoading)
});
Upvotes: 0
Views: 44
Reputation: 1520
You can use redux-thunk to dispatch actions synchronously. You can also use the lifecycle methods "componentDidUpdate" or "getDerivedStateFromProps" to check the props and state and execute other functions based on their current value.
Codesandbox to demonstrate using componentDidUpdate - https://codesandbox.io/s/simplest-redux-example-pwm7e
Upvotes: 1