问题描述
所以我试图从子组件(子组件调用 axios)更新父组件中的状态,然后我想通过路由将此状态从父组件发送到另一个子组件但状态没有得到更新,也许因为我在路由中首先发送状态,有人能告诉我在 react-route 中发送更新状态的正确方法吗?
代码片段:
const ParentComponent = () =>{
const [info,setInfo] = useState([]);
...
...
...
<Switch>
<Route
exact
path="/child1Component"
render={() => <Child1Component setData={setInfo} />}
/>
<Route
exact
path="/child2Component"
render={() => (
<Child2Component dataApp={info} />
)}
/>
</Switch>
}
const Child1Component = (props) =>{
axios
.get("URL",{
})
.then((response) => {
props.setData(response.data);
setLoading(false);
});
...
...
...
}
我希望更新后的状态值作为 Child2Component 中的道具传递,目前它不更新初始状态。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)