How to call a method inside getDerivedStateFromProps in React?

Posted by Jessica Taylor | Updated on

Want to call a method inside getDerivedStateFromProps in React? For this situation, it was good for the OP to use componentDidUpdate but I found myself needing getDerivedStateFromProps so I had to make my custom function static as well and call it using the class’ name inside getDerivedStateFromProps. Something like this:

To clarify, this is updating the component’s state on load as well as when new props arrive. This definitely took me back to my typed-language days. Hope it helps!


If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.

Related Questions & Answers