gpt4 book ai didi

reactjs - 如何从react-router onEnter hook连接到redux store?

转载 作者:行者123 更新时间:2023-12-03 13:20:06 26 4
gpt4 key购买 nike

我想使用react-router的onEnter钩子(Hook)在路由加载时检查我的redux存储中的变量。

根据文档,onEnter:

...receives the next router state as its first argument. The replaceState function may be used to trigger a transition to a different URL.

没有商店经过。是否可以从这个钩子(Hook)内连接到 redux 存储?我尝试了各种各样的方法,但找不到实现这一目标的方法。

我正在使用的版本:

"react-redux": "^4.0.0",
"react-router": "1.0.0-rc1",
"redux": "^3.0.0",

最佳答案

让您路由一个在参数中接受存储的函数

export default (store) => {
return (
<Route handler={App} path="/">
<Route path="mypath" component={handler} onEnter={myHook(store)}>
<Route>
)
};

在转换 Hook 中执行相同的操作

export const myHook = (store) => {
return (location, replaceWith) => {
// Do something with your store
}
};

然后假设您在同一个文件中创建商店和路由器

const router = (
<Provider store={store}>
<Router history={history}>
{routes(store)}
</Router>
</Provider>
);

关于reactjs - 如何从react-router onEnter hook连接到redux store?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33488090/

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