axios - 从 axios 获取并使用 react-router 后,React 状态未在功能组件中更新
问题描述
所以我试图从子组件(子组件调用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 中的道具传递,现在它没有更新初始状态。
解决方案
推荐阅读
- security - 什么是 Power Client 中央存储设施 (powerclientctf) 端口 2443
- javascript - 数组值编辑使所有值都相同
- operators - XNOR 是否有任何“模”等效表示?
- xml - XSLT 将嵌套的 xml 元素重写为一个带有逗号分隔值字符串的元素
- docker - 关于何时选择虚拟机或物理机而不是容器的指南
- java - 如何从 docker 环境变量中覆盖 java jar application.properties
- javascript - 有没有办法调试如何通过 Chrome 开发者工具设置 HTTP 标头?
- python - ModuleNotFoundError:没有名为“fontawesomefree”的模块
- python - 使用 xml.etree.ElementTree 解析一些元素的问题
- react-native - Expo Android - RN Modal 将导航栏颜色改为白色