【问题标题】:Universal redux: browserhistory error: cannot read property 'listen' of undefined通用 redux:浏览器历史错误:无法读取未定义的属性“侦听”
【发布时间】:2017-02-18 01:08:23
【问题描述】:

我正在尝试使用 express.js 服务器为我的 react-redux 应用程序实现通用渲染,我正在关注以下教程:http://redux.js.org/docs/recipes/ServerRendering.html

每当我尝试按照教程中所述将 rootreducer 或 App 文件导入 express server.js 文件时,我都会收到以下错误:

\node_modules\react-router-redux\lib\sync.js:105
 unsubscribeFromHistory = history.listen(handleLocationChange);
                              ^
TypeError: Cannot read property 'listen' of undefined
at syncHistoryWithStore (C:\Users\user pc\Desktop\citydrill\node_modules\react-router-redux\lib\sync.js:105:35)

这是我的商店文件相关代码:

import { syncHistoryWithStore } from 'react-router-redux';
import { browserHistory } from 'react-router';

const store = createStore(rootReducer, initialState, compose(
    applyMiddleware(thunk)
));

export const history = syncHistoryWithStore(browserHistory, store);

然后我只是像这样在 clientApp 中使用历史常量:

const router = (
<Provider store={store}>
    <Router history={history}>
        <Route name="Home" path="/" component={App}>
            <IndexRoute name="Home" component={Landing}></IndexRoute>
        </Route>
    </Router>
</Provider>

)

除了 redux.js.org 中的教程之外,我也尝试了许多不同的教程,但我总是卡在这一点上,因为我总是必须从我的 express server.js 文件中的客户端代码中导入一些文件,并且无论何时我这样做我总是得到同样的错误。

如何实现通用 redux?我究竟做错了什么?我遵循了这么多教程,但没有一个提到这个错误,为什么它对这些人有用,但在我的应用程序中却没有?

感谢您的帮助

【问题讨论】:

    标签: javascript node.js reactjs redux react-router-redux


    【解决方案1】:

    您不能在服务器端使用browserHistory,因为名称表明它适用于浏览器。请改用memoryHistory:

    https://github.com/ReactTraining/react-router/blob/master/docs/guides/Histories.md

    【讨论】:

    • 那个网站不是很详细,可以分享一个例子吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 2021-07-18
    • 2018-02-04
    • 2017-11-14
    • 2019-07-25
    • 2020-03-27
    • 1970-01-01
    相关资源
    最近更新 更多