gpt4 book ai didi

reactjs - react 路由器,history.push 之后的代码运行?

转载 作者:行者123 更新时间:2023-12-03 23:07:05 25 4
gpt4 key购买 nike

在你的 clickHandler 上说

你做

// url change will cause this component to be dismounted and a new component will mount
history.push(url)

// do some more work
dispatch(someMoreWork())
push 调用后代码会发生什么变化?代码能保证运行吗?

最佳答案

历史导航仅在执行 javascript 中事件队列中的同步操作后完成。

在您的情况下,javascript 将执行您调用 history.push 的函数中的所有内容,然后导航到新的 Route,这将是一个可预测的行为

您可以通过执行调度操作然后访问呈现组件中的值来简单地验证上述内容。

Check this demo 通过在 history.push 之后调度 Action 并访问渲染路由中的结果 redux 状态来演示上述行为

关于reactjs - react 路由器,history.push 之后的代码运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61846871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com