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()
服務器端和客戶端都只調用一次,在初始化渲染執行之前立刻調用。如果在這個方法內調用setState
,render()
將會感知到更新后的 state,將會執行僅一次,盡管 state 改變了。