Michal16511
Michal16511

Reputation: 87

How do execute callback when state has been updated in redux?

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

Answers (1)

Vijay Venugopal Menon
Vijay Venugopal Menon

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

Related Questions