Angular vs Vue vs React

React getDerivedStateFromProps method | Lifecycle Components

The getDerivedStateFromProps Method -
The getDerivedStateFromProps() method is called before rendering the elements in the DOM.

The getDerivedStateFromProps() method is called right before the render method and after the constructor method.

We can set the state object based on the initial props and it takes state as an argument, and returns an object with changes to the state.

Explore to Understand  - React Lifecycle Components

We can update the constructor state in the getDerivedStateFromProps() method.

As an Example,
public class Header extends React.Component {
  constructor(props) {
    super(props);
    this.state = {msg: "welcome!"};
  }

  static getDerivedStateFromProps(propsstate) {
    return {msg: props.yourMsg };
  }

  render() {
    return (
      <h1>{this.state.msgyou anil</h1>
    );
  }
}
ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

www.code-sample.com/. Powered by Blogger.
^