2017-02-13 123 views
0

创建存储时,我是新来的反应,终极版,并试图遵循一个教程,是有点老了,所以当我尝试运行此代码我得到错误:阵营路由器,终极版的错误与中间件

未捕获错误:预计路由状态可用作state.routing或作为自定义表达式,您可以在syncHistoryWithStore()选项中指定为selectLocationState。确保您已通过combineReducers或您用于隔离减速器的任何方法将routerReducer添加到您商店的减速器中。

任何提示? :)

index.js

import React from 'react' 
import ReactDOM from "react-dom" 
import { createStore, applyMiddleware } from 'redux' 
import { Provider } from 'react-redux' 
import { Router, Route, browserHistory } from 'react-router' 
import { syncHistoryWithStore, routerReducer } from 'react-router-redux' 
import App from './containers/App' 
import rootReducer from './reducers/reducers' 
import thunkMiddleware from 'redux-thunk' 
import api from './middleware/api' 

let createStoreWithMiddleware = applyMiddleware(thunkMiddleware, api)(createStore) 

let store = createStoreWithMiddleware(rootReducer) 

let history = syncHistoryWithStore(browserHistory, store) 

let rootElement = document.getElementById('root') 

ReactDOM.render(
    <Provider store={store}> 
    <Router history={history}> 
     <Route path='/' component={App} /> 
    </Router> 
    </Provider>, 
    rootElement 
) 

我reducers.js看起来是这样的:

import { combineReducers } from 'redux' 

import Auth from './auth' 
import Quotes from './quotes' 

const rootReducer = combineReducers({ 
    auth: Auth, 
    quotes: Quotes 
}) 

export default rootReducer 
+0

你可以包括你的'/减速/ reducers'文件? –

回答

1

您必须添加routerReducer()进行同步工作的反应,路由器终极版。

这个reducer函数存储来自历史的位置更新。如果 使用combineReducers,它应该嵌套在路由键下。

在您的联合减速器中包含以下内容。

import { routerReducer } from 'react-router-redux'; 

export const reducers = combineReducers({ 
    ... // other reducers 
    routing: routerReducer 
}); 

所以你减速器文件看起来像:

import { routerReducer } from 'react-router-redux'; 
import { combineReducers } from 'redux' 

import Auth from './auth' 
import Quotes from './quotes' 

const rootReducer = combineReducers({ 
    auth: Auth, 
    quotes: Quotes, 
    routing: routerReducer 
}) 

export default rootReducer 
+0

是的,这是正确的谢谢!我仍然有早先的建议“撰写”在所以它扔了错误,但现在它与这:) –