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

getDerivedStateFromProps example

GetDerivedStateFromProps Example
The getDerivedStateFromProps life-cycle method is invoked before calling the render method, both during the initial mounting phase and the update phase. This method returns an object which will be updated in the components state, if no state change is required this method returns null.

The following example uses getDerivedStateFromProps() to check the value of the prop "parentColor", compare the previous value of the prop (saved in the state variable prevColor). If the prop value is different from the previous value then the state value of "bgColor" is updated, which in-turn 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

getDerivedStateFromProps example

×

Subscribe to C# Guide

Get updates delivered right to your inbox!

Thank you for your subscription

×