Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

componentWillReceiveProps example

ComponentWillReceiveProps Example
The componentWillReceiveProps life-cycle method is used to check any change in prop values and update the state of the component if required. This life-cycle method has access to both the previous set of props and new set of props hence we can compare them and update the state if required.

The componentWillReceiveProps life-cycle method will get deprecated, React 16.3 introduced a new life-cycle method getDerivedStateFromProps, which should be used to check prop value changes going forward.

The following example uses componentWillReceiveProps() to check the value of the prop "parentColor", compare the previous value of the prop and the current value of the prop and updates the state if the prop was changed. Any change in state value updates the color of the box displayed in the UI.


This post first appeared on C# Guide, please read the originial post: here

Share the post

componentWillReceiveProps example

×

Subscribe to C# Guide

Get updates delivered right to your inbox!

Thank you for your subscription

×