Practice
If a component's
change without the component being remounted, the new prop value will never be rendered, because the constructor function will never update the component's current state. State is initialised from props only when the component is first created.
The component below will not render the updated input value:
class MyComponent extends React.Component {
constructor(props) {
super(props)
this.state = {
records: [],
inputValue: this.props.inputValue
};
}
render() {
return <div>{this.state.inputValue}</div>
}
}
Using the prop inside the render method will update the value:
class MyComponent extends React.Component {
constructor(props) {
super(props)
this.state = {
record: []
}
}
render() {
return <div>{this.props.inputValue}</div>
}
}
Comments