Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how and when to call a react component methods after state change from redux

In a single react component a user clicks a button => which calls a method => triggers an action => async fetch => reducer updates state => component receives new props.

back in the original component that triggered the action I have been using:

componentWillReceiveProps(nextProps){
    if(nextProps.someProp !== this.props.someProp){
        //ok new prop is here
        this.someMethod(nextProps.someProp);
    }
}

Am I going about this in the right way?

It just seems somewhat clunky and detached as a callback mechanism from the user action or change of state. It just makes following the logical flow of the component harder once there are a few of these, I have a component with 3 of these and already think it's not as easy to reason about especially when they are part of a related flow a > b > c . And I have ended up with this kind of thing:

componentWillReceiveProps(nextProps){

    if(this.patchJavaScriptWillLoad(nextProps)){
        this.createPatchInstance();
        // method fires an action which will also result in state change that triggers the below.
    }

    if(this.patchInstanceWillBeReady(nextProps)){
        this.startPatchAudio(nextProps.webAudioPatch.instance);
        // method fires an action which will also result in state change that triggers the below.
    }

    if(this.patchParametersWillChange(nextProps)){
        this.updateWebAudioPatchParameters(nextProps.webAudioPatchParameters);
    }
}

// abstracted away if conditions to make componentWillReceiveProps more readable. 

But is this how it should be done or is this a symptom of not moving enough logic to action creators?

like image 333
njorlsaga Avatar asked Jul 25 '16 13:07

njorlsaga


People also ask

What happens when Redux state changes?

The fact that the Redux state changes predictably opens up a lot of debugging possibilities. For example, using time travel makes it possible to travel back and forth between different states instead of having to reload the whole app in order to get back to the same place.

What happens after the component state is being updated?

Changes in state and/or props will both trigger a re-render of our React component. However, changes in state can only happen internally due to components changing their own state. Thus, a component can trigger changes in its own state. A component cannot change its props.

What happens as soon as the state of the React component get changed?

By default, when your component's state or props change, your component will re-render. If your render() method depends on some other data, you can tell React that the component needs re-rendering by calling forceUpdate() .

How do I render a component after state change?

React components automatically re-render whenever there is a change in their state or props. A simple update of the state, from anywhere in the code, causes all the User Interface (UI) elements to be re-rendered automatically. However, there may be cases where the render() method depends on some other data.


1 Answers

Coming back to my own question years later now.

If I can use a functional component, I would use the react hook useEffect. If the logic can be externalised then perhaps in a saga.

useEffect(() => {
  methodToCallIfPropChanges()
}, [watchedProp]);
like image 130
njorlsaga Avatar answered Sep 21 '22 14:09

njorlsaga