最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - what is the difference between getDefaultProps and getInitialState react js - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 7

getInitialState

  • 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.

发布评论

评论列表(0)

  1. 暂无评论