微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从 axios 获取并使用 react-router 后,功能组件中的 React 状态未更新

如何解决从 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 中的道具传递,目前它不更新初始状态。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。