简体   繁体   English

React-router导致无法读取未定义的属性'listen'

[英]React-router causes Cannot read property 'listen' of undefined

I faced strange js exception when rendered webpack build with react-router on the server side: 当在服务器端使用react-router渲染webpack时,我遇到了奇怪的js异常:

Cannot read property 'listen' of undefined

How can I solve it? 我该如何解决?

browserHistory is the code resulted by createRouterHistory , which returns undefined if DOM is not defined. browserHistorycreateRouterHistory生成的代码,如果undefined DOM,则返回undefined That's why you are having this error. 这就是你遇到这个错误的原因。

Solution is to import useRouterHistory and replace it somehow with createMemoryHistory , which intention is to be used for server side rendering. 解决方法是导入useRouterHistory并以某种方式将其替换为createMemoryHistory ,其意图用于服务器端呈现。 Implementation for webpack: webpack的实现:

# application.js
import { Router, useRouterHistory } from "react-router"
import { createHistory } from "history"

const browserHistory = useRouterHistory(createHistory)({ queryKey: false })

<Router history={history}>
  ...bla-bla
</Router>

# webpack.config.server.js - sends this code to react server
new webpack.NormalModuleReplacementPlugin(
  /createBrowserHistory/,
  require.resolve("./node_modules/react-router/lib/createMemoryHistory.js")
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 react-router:无法读取undefined的属性&#39;push&#39; - react-router: Cannot read property 'push' of undefined React-Router-未捕获的TypeError:无法读取未定义的属性“ route” - React-Router - Uncaught TypeError: Cannot read property 'route' of undefined 反应路由器“无法读取未定义的属性&#39;推送&#39;” - react-router " Cannot read property 'push' of undefined" React-Router 错误“无法读取未定义的属性‘位置’” - React-Router Error "Cannot read property 'location' of undefined" 无法读取未定义的反应路由器测试的属性“ contextTypes” - Cannot read property 'contextTypes' of undefined, react-router testing React-Router无法读取未定义的属性字符串 - React-Router cannot read property string of undefined 反应路由器:未捕获的TypeError:无法读取未定义的属性&#39;func&#39; - React-router: Uncaught TypeError: Cannot read property 'func' of undefined react-router无法读取undefined的属性&#39;push&#39; - react-router Cannot read property 'push' of undefined react-router Uncaught TypeError:无法读取未定义的属性'toUpperCase' - react-router Uncaught TypeError: Cannot read property 'toUpperCase' of undefined 未捕获的TypeError:无法在javascript / React-router / React中读取未定义的属性&#39;forEach&#39; - Uncaught TypeError: Cannot read property 'forEach' of undefined in javascript / React-router / React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM