【发布时间】:2019-08-03 09:12:48
【问题描述】:
我在 React 中有(例如)两个组件。第一个App.js 是父组件。它将一些值传递给子组件Child.js。在child.js, 中,它通过props 接收值并使用axios 调用结果更新一些state 变量。这很好用。
现在我需要在App.js 中获取更新结果值。如何在App.js 中获得该值?
App.js
this.state ({ ...
examResult: null // need to update this with the child component result.
})
<ChildComponent
Id={this.state.StudentId}
Name={this.state.StudentName}
/>
Child.js
state {
examResult: null
}
...
componentDidMount()
{
const {Id, Name} = this.props;
axios.get( .... //To get the Result
this.setState(
{ examResult: examResult} //Update the result to state variable. It wors fine. I need to pass this value to App.js
)
}
【问题讨论】:
标签: javascript reactjs components parent-child