我如何使用 history.push('/') 然后在该组件中调用一个函数?

How can I use history.push('/') then once there invoke a function in that component?

我有 2 个独立的组件。 1 个组件(在不同的路由中)可以访问会话(登录页面)和另一个组件(仪表板)。我希望能够使用登录路由 ('/') 中的功能,但我只需要在另一个组件满足条件时才这样做。

我如何使用 history.push('/') 并在到达那里后调用该组件中的函数?

这是因为我在 '/' 路由中有会话数据,我想使用该组件中的函数来访问该会话。但我不能在另一条路线上做到这一点。 :/

任何替代方案也会有所帮助,谢谢 :c

如果是 https://github.com/reacttraining/history history,您可以将状态对象作为第二个参数传递给 push()

例如来自 github 页面:

// Push a new entry onto the history stack with a query string
// and some state. Location state does not appear in the URL.
history.push('/home?the=query', { some: 'state' })