如何用 redux 和 react-router 组织状态?

How to organize state with redux and react-router?

Redux提倡使用单一状态的单一商店。然而,使用 react-router 你会得到多个页面,每个页面都有自己的顶级根组件。

应该如何将 redux 与具有多个页面的 react-router 应用连接起来? React-router 1.0 不再允许您将 props 传递给路由,因此使路由器成为包含所有页面状态的顶级组件不再可能也不可行。

如果您使用的是 redux + react-router,我强烈建议您也使用 redux-router - 这将允许您在商店中保留路线信息 - 我通常有以下设置。

redux-router: ^1.0.0-beta3 / 反应路由器": ^1.0.0-rc1 / redux: "^3.0.2/ 反应:“^0.14.0

//index.js [入口点]

import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route } from 'react-router';
import { Provider } from 'react-redux';
import createStore from './utils/create-store';
import routes from './bootstrap/routes';
import { ReduxRouter } from 'redux-router';

const store = createStore(routes);

ReactDOM.render(
    <Provider store={store}>
        <ReduxRouter>
            {routes}
        </ReduxRouter>
    </Provider>,
    document.getElementById('root')
);

// 创建-store.js

import { applyMiddleware, createStore, combineReducers, compose } from 'redux';
import * as reducers from '../reducers/index';
import promiseMiddleware from './promise-middleware';
import { routerStateReducer, reduxReactRouter } from 'redux-router';
import history from './history'

/**
 * Sets up the redux store.  Responsible for loading up the reducers and middleware.
 *
 * @param routes
 */
export default function create(routes) {
    const composedReducers = combineReducers({
        router: routerStateReducer,
        ...reducers
    });
    const finalCreateStore = compose(applyMiddleware(promiseMiddleware),
        reduxReactRouter({
            routes,
            history
        }))(createStore);
    let store = finalCreateStore(composedReducers);
    return store;
}

// routes.js

import React from 'react';
import { Route } from 'react-router';
import  App from './app';

module.exports = (
    <Route component={App}>
        ...all your routes go here
    </Route>
);

// app.js

import React, { PropTypes } from 'react';
import { connect } from 'react-redux';

export default class App extends React.Component {

    render() {
        const { props: { children } } = this;
        return (
            <div className="app-container">
              {children}
            </div>
        );
    }
}

正如您所见,有一个高阶组件包裹了其余的路由

Redux-router不再兼容react-router v4,上述方案将无法使用

因此,我建议使用 redux-little-router。与 redux-router 不同,它不依赖于 react-router,而是它的替代品。

有了它,您可以执行以下操作:

从您的异步操作推送到新路由:

export function navigateAbout() {
  return (dispatch) => {
    dispatch(push('/about'))
  }
}

在 redux-dev-tools 中查看你的路由器详细信息(当前路径、查询字符串和参数),并像访问任何其他 prop 一样从你的存储中访问这些详细信息:

function mapStateToProps(state) {
  return {
    string: state.router.query.string
  }
}

PS。可以参考或使用this react boilerplate 已经实现的redux-little-redux