【发布时间】:2021-02-06 19:26:57
【问题描述】:
这是我的父组件
import React, { Component } from 'react';
class Parent extends Component {
state={
myData: ""
}
getDataOne=(data)=>{
console.log(data)
this.setState({ myData: data });
}
render(){
return (
<div>
<ChildComponentOne onGetSearch={this.getDataOne} />
<ChildComponentTwo dataOne={this.props.location.state.searchData} dataTwo={this.state.myData} />
</div>
);
}
}
export default Parent;
事情就是这样。
-
我最初从重定向到 this 的组件中获取数据作为道具。那 是叫dataOne的数据,我把它传给了ChildComponentTwo。
-
然后,我现在尝试将另一个从 ChildComponentOne 获得的数据传递给 ChildComponentTwo,其中另一个名为 dataOne 的道具名称,但是当尝试通过 this.props 从 ChildComponentTwo 访问它时它返回空字符串。
我可以访问的唯一数据是 dataOne。
我哪里弄错了?
【问题讨论】:
-
在将 state 变量发送到 prop 之前,您是否检查过控制台记录状态变量的值?是否在函数中设置
-
是的,我做到了,它显示在 Parent 组件中,但在 ChildComponentTwo 中返回空
-
可能是渲染时间的问题
-
我也是这么想的,你能帮忙用什么技巧让它工作吗?
-
当然,请检查答案,希望它会工作。我刚刚渲染了第一个孩子,所以它会在渲染第二个孩子之前腾出一些时间来更新状态
标签: reactjs react-props react-component