繁体   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