I am new in react js.
getDefaultProps () {
return {
backgroundColor: 'gray',
height: 200,
width: 200
}
},
I've same values for getDefaultProps
and for getInitialState
:
getInitialState () {
return {
backgroundColor: 'gray',
height: 200,
width: 200
}
},
What is the logical difference in between both. which should supposed to override or which one will execute first.
Thanks.
I am new in react js.
getDefaultProps () {
return {
backgroundColor: 'gray',
height: 200,
width: 200
}
},
I've same values for getDefaultProps
and for getInitialState
:
getInitialState () {
return {
backgroundColor: 'gray',
height: 200,
width: 200
}
},
What is the logical difference in between both. which should supposed to override or which one will execute first.
Thanks.
Share Improve this question edited Oct 19, 2016 at 14:10 Zakaria Acharki 67.5k15 gold badges78 silver badges105 bronze badges asked Oct 19, 2016 at 14:00 MuhammadMuhammad 3,2506 gold badges44 silver badges71 bronze badges 1- 1 google./… – StackOverMySoul Commented Oct 19, 2016 at 14:06
3 Answers
Reset to default 7getInitialState
- The object
**getInitialState()**
Invoked once before the ponent is mounted. The return value will be used as the initial value of this.state.
Note: This method is not available on ES6 class ponents that extend React.Component. For more information, please read our documentation about ES6 classes.
getDefaultProps
- The object
**getDefaultProps()**
Invoked once and cached when the class is created. Values in the mapping will be set on this.props if that prop is not specified by the parent ponent (i.e. using an in check). - This method is invoked before any instances are created and thus
cannot rely on this.props. In addition, be aware that any plex
objects returned by
getDefaultProps()
will be shared across instances, not copied.
getDefaultProps is for default props,if you don't inject this prop ,it will work .
getInitialState is for ini state,before the ponent is mounted.
In fact, what matter is the difference between props and state ,once you understand their differences, their default value's defference is easy to understand.
getDefaultProps()
is for Properties.
getInitialState()
is for States.