[英]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.