如何在路由反应路由器中使用 redux 状态? [英] How use redux state with routes react-router?

查看:58
本文介绍了如何在路由反应路由器中使用 redux 状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了我的路线

<Provider store={store}>
    <Router history={browserHistory}>
        <Route path="/" component={App}>
        <Route path="/redux" component={redux} />
    </Router>
</Provider>

我的问题是当我从 redux 组件分派一个动作时,道具在 /redux 路线中改变了,但在 / 中总是初始状态.请问谁能帮帮我?

My problem is when I dispatch an action from redux component , the props changed in the /redux route but in the / always the initial state. Please who can help me ?

推荐答案

尝试使用 react-router-redux (https://github.com/reactjs/react-router-redux) 将 redux 存储与 react-router<同步/代码>.

Try to use react-router-redux (https://github.com/reactjs/react-router-redux) to synchronise redux store with react-router.

import { browserHistory } from 'react-router';
import { syncHistoryWithStore, routerReducer } from 'react-router-redux';
import { createStore, combineReducers } from 'redux';

// Add router reducer to your store on the `routing` key
const store = createStore(
  combineReducers({
    ...reducers,
    routing: routerReducer
  })
)
// Create enhanced history that syncs navigation events with the store
const history = syncHistoryWithStore(browserHistory, store);

...

<Provider store={store}>
    <Router history={history}>
        <Route path="/" component={App}>
        <Route path="/redux" component={redux} />
    </Router>
</Provider>

这篇关于如何在路由反应路由器中使用 redux 状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆