反应路由器,history.push 之后的代码运行?

eug*_*ene 5 reactjs react-router react-native

在你的 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())
Run Code Online (Sandbox Code Playgroud)

push调用后代码会发生什么?代码能保证运行吗?

Shu*_*tri 6

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

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

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

检查此演示该演示通过在 history.push 之后调度操作并访问渲染路由中的结果 redux 状态来演示上述行为