score:7

Accepted answer

getinitialstate

  • the object **getinitialstate()** invoked once before the component is mounted. the return value will be used as the initial value of this.state.

note: this method is not available on es6 class components 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 component (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 complex objects returned by getdefaultprops() will be shared across instances, not copied.

score:-3

getdefaultprops() is for properties.

getinitialstate() is for states.

score:5

getdefaultprops is for default props,if you don't inject this prop ,it will work .

getinitialstate is for ini state,before the component 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.


Related Query

More Query from same tag