[英]How to use state or props of a function in another function in react js
我有render函數,可以在其中使用state
和props
,當我從此處調用另一個方法時,我可以傳遞這些值並使其他函數可以訪問它們。 但是,我想從componentDidMount
函數調用我的方法,並消耗可在渲染中用完的state/props
。 當我從componentDidMount
調用函數時, state/prop
數據仍未定義。
render
render : function(){
var customState = this.state.data;
var customProp = this.props.otherData;
},
componenDidMount
componentDidMount : function(){
this.myCustomFunction();
},
myCustomFunction
mycustomFunction : function(){
var stateData = this.state.data.name; //undefined
},
現在,如何在這里傳遞狀態數據?
你在做什么是正確的。
mycustomFunction : function(){
var stateData = this.state.data.name; //undefined
}
this.state.data.name可能是未定義的,因為您沒有設置它。 嘗試僅調試this.state.data
或對其進行控制台,然后檢查數據是否存在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.