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