簡體   English   中英

如何在React JS中的另一個函數中使用函數的狀態或道具

[英]How to use state or props of a function in another function in react js

我有render函數,可以在其中使用stateprops ,當我從此處調用另一個方法時,我可以傳遞這些值並使其他函數可以訪問它們。 但是,我想從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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM