react - redux - 路由器设置问题

时间:2017-06-05 15:33:44

标签: reactjs redux react-router

任何人都可以帮我弄清楚如何让这个设置正常工作。

import 'babel-polyfill';
    import React from 'react';
    import ReactDOM from "react-dom";
    import {Provider} from 'react-redux';
    import {createStore, applyMiddleware} from 'redux';
    import thunk from 'redux-thunk';
    import promise from 'redux-promise';
    import createLogger from 'redux-logger';
    import allReducers from './reducers';
    import App from './components/App';
    import createHistory from 'history/createBrowserHistory'
    import { Route } from 'react-router-dom'
    import { ConnectedRouter, routerMiddleware, push } from 'react-router-redux'

    const logger = createLogger();

    // Create a history of your choosing (we're using a browser history in this case)
    const history = createHistory()

    // Build the middleware for intercepting and dispatching navigation actions
    const middleware = routerMiddleware(history)

    // Add the reducer to your store on the `router` key
    // Also apply our middleware for navigating
    const store = createStore(
    allReducers,
    applyMiddleware(middleware, thunk, promise, logger)
    )

    ReactDOM.render(
        <Provider store={store}>
            <ConnectedRouter history={history}>
            <div>
                <Route exact path="/" component={App}/>
            </div>
            </ConnectedRouter>
        </Provider>,
        document.getElementById('root')
    );

我似乎收到了一个错误。警告:React.createElement:type无效 - 期望一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:undefined。您可能忘记从其定义的文件中导出组件。

2 个答案:

答案 0 :(得分:1)

确保Components层次结构中的所有App都已正确导出为默认导出。

另请查看从react-router Components开始的地方。尝试从Link模块导入react-router组件时常见错误,但它仅存在于react-router-dom模块中。

答案 1 :(得分:1)

感谢所有反馈,答案是ConnectedRouter未定义。我现在已正确修复此导入。