What is the difference between getDefaultProps and getInitialState js

I am new to js reaction.

getDefaultProps () { return { backgroundColor: 'gray', height: 200, width: 200 } }, 

I have the same values ​​for getDefaultProps and for getInitialState :

 getInitialState () { return { backgroundColor: 'gray', height: 200, width: 200 } }, 

What is the logical difference between the two. which was to be redefined or which will be executed first.

Thanks.

+5
source share
2 answers

getInitialState

  • Object **getInitialState()** Called once before the component is installed. The return value will be used as the initial value of this.state.

Note. This method is not available for components of the ES6 class that are extended by React.Component. For more information, please read our ES6 class documentation.

getDefaultProps

  • Object **getDefaultProps()** Called once and cached when the class is created. The values ​​in the mapping will be set on this .props if this prop is not specified by the parent component (that is, using in check).
  • This method is called before any instances are created and thus cannot rely on this.props. Also, keep in mind that any complex objects returned by getDefaultProps() will not be shared copies.
+3
source

getDefaultProps - for the default props, if you do not enter this option, it will work.

getInitialState for ini state before the component is installed.

In fact, what is the difference between details and status , as soon as you understand their differences, their default default is easy to understand.

+3
source

Source: https://habr.com/ru/post/1258476/


All Articles