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