Skip to content

Instantly share code, notes, and snippets.

@WrathChaos
Created November 7, 2019 20:29
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save WrathChaos/c476aac7c102ea2f01fd955c05b685f8 to your computer and use it in GitHub Desktop.
Save WrathChaos/c476aac7c102ea2f01fd955c05b685f8 to your computer and use it in GitHub Desktop.
React Hooks: componentWillReceiveProps
// Class
componentWillReceiveProps(nextProps) {
    if (nextProps.data !== this.props.data) {
        console.log('Prop Received: ', nextProps.data);
    }
}

// React Hooks: componentWillReceiveProps
useEffect(() => {
    console.log('Prop Received: ', props.data);
}, [props.data])
@anandgupta193
Copy link

@WrathChaos: What if I want to call both shouldComponentUpdate and componentWillReceiveProps both?

If I will implement shouldComponentUpdate using React.memo then the order will be
shouldComponentUpdate -> componentWillReceiveProps

but ideally, in class-based components, componentWillReceiveProps should get called before shouldComponentUpdate

@WrathChaos
Copy link
Author

WrathChaos commented Aug 10, 2020

@anandgupta193
Actually you need to decide which one you need to put the code with this lifecycle :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment