The constructor() method

The constructor method of React Component gets invoked first when the component is mounted. Here, we can set the state of the component.

Here's an example of constructor in React.Component:

constructor(props) {
super(props);
this.state = {
value: props.initialValue
};
}
Using this.props inside the constructor, we need to call super(props) to access and call functions of parents; otherwise, you will get this.props undefined in the constructor because React sets the .props on the instance from outside immediately after calling constructor, but it will not affect when you are using this.props inside the render method.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset