React的componentWillMount和getInitialState


  getInitialState: function(){
      return {objectEntity: this.props.data.ObjectList[this.props.subclaimIndex],
              currentlossCause: this.props.data.LossCause,
              codeTableSubclaimType: this.props.data.CodeTableSubclaimType,
              codeTableDamageType: this.props.data.CodeTableDamageType};
  },

  componentWillMount: function(){
      ClaimHandlingStore.listen(this.onStoreStateChange);
      if(this.props.forModify == "true"){
          var objectEntity = this.props.data.ObjectList[sessionStorage.getItem("ModifySubclaim_index")];
          this.setState({objectEntity, objectEntity});
      }
  }, 

在React的componentWillMount使用setState方法如果所设置的stater的key在getInitialState方法中已存在,则报错如下:

 D:/project/GC40/src/claim/module/registration/unicorn-claim-registration-common-ui/src/module/registration/common/component/SubclaimInfo.js
Module parse failed: D:\project\GS40\src\unicorn\node_modules\babel-loader\index.js!D:\project\GC40\src\claim\module\registration\unicorn-claim-registration-common-ui\src\module\registration\common\component\SubclaimInfo.js Line 61: Duplicate data property in object literal not allowed in strict mode
You may need an appropriate loader to handle this file type.
|         if (this.props.forModify == "true") {
|             var objectEntity = this.props.data.ObjectList[sessionStorage.getItem("ModifySubclaim_index")];
|             this.setState({ objectEntity: objectEntity, objectEntity: objectEntity });
|         }
|     },

 

 

生命周期方法

许多方法在组件生命周期中某个确定的时间点执行。

getInitialState #

object getInitialState() 

在组件挂载之前调用一次。返回值将会作为 this.state 的初始值。

挂载: componentWillMount

componentWillMount() 

服务器端和客户端都只调用一次,在初始化渲染执行之前立刻调用。如果在这个方法内调用setStaterender() 将会感知到更新后的 state,将会执行仅一次,尽管 state 改变了。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM