Skip to main content
InterviewHubPRO
Section Overview
Q344

Question 344 of 407

Part 9: React Hooks Deep Dive (262 - 316)

85%
React Core & Hooks (407 Qs)•Part 9: React Hooks Deep Dive (262 - 316)

What is the purpose of getDerivedStateFromProps() lifecycle method?

junior LevelCode ExampleComponentsHooksLifecycle

The new static getDerivedStateFromProps() lifecycle method is invoked after a component is instantiated as well as before it is re-rendered. It can return an object to update state, or null to indicate that the new props do not require any state updates.

javascript
    class MyComponent extends React.Component {
      static getDerivedStateFromProps(props, state) {
        // ...
      }
    }
scss
This lifecycle method along with `componentDidUpdate()` covers all the use cases of `componentWillReceiveProps()`.