[英]TypeError when using React: Cannot read property 'firstChild' of undefined
Sometimes, when using React libraries, such as react-router , I get this error: 有时,当使用React库,例如react-router时 ,我收到此错误:
Uncaught TypeError: Cannot read property 'firstChild' of undefined 未捕获的TypeError:无法读取未定义的属性“firstChild”
/~/react-router/~/react/lib/ReactMount.js?:606 /~/react-router/~/react/lib/ReactMount.js?:606
How do I fix it? 我如何解决它?
This error is commonly caused by two versions of React loaded alongside . 此错误通常是由两个版本的React一起加载引起的。
For example, if you npm install
a package that requires a different React version and puts it into dependencies
instead of peerDependencies
, it might install a separate React into node_modules/<some library using React>/node_modules/react
. 例如,如果您npm install
了一个需要不同React版本并将其放入dependencies
而不是peerDependencies
,它可能会node_modules/<some library using React>/node_modules/react
将一个单独的React安装到node_modules/<some library using React>/node_modules/react
。
Two different Reacts won't play nicely together (at least yet). 两种不同的React不能很好地一起玩(至少还没有)。
To fix it, just delete node_modules/<some library using React>/node_modules/react
. 要修复它,只需node_modules/<some library using React>/node_modules/react
删除node_modules/<some library using React>/node_modules/react
。
If you see a library putting React in dependencies
instead of peerDependencies
, file an issue. 如果您看到一个库将React放在dependencies
而不是peerDependencies
,则提出问题。
In case anyone has this issue having npm link
ed two modules depending on react, I found a solution... 如果任何人有这个问题有npm link
ed两个模块取决于反应,我找到了一个解决方案......
Let's say you have Parent depending on React, and Child depending on react. 假设您有父母取决于React,而Child取决于反应。 When you do: 当你这样做时:
cd ../child npm link cd ../parent npm link child
This causes this problem, because parent and child will each load their own instance of React. 这会导致这个问题,因为父和子都会加载自己的React实例。
The way to fix this is as follows: 解决这个问题的方法如下:
cd parent cd node_modules/react npm link cd ../../../child npm link react
This ensures your parent project supplies the react dependency, even when linked, which is how npm would resolve the dependency when you are unlinked. 这可确保您的父项目提供反应依赖项,即使在链接时也是如此,这是npm在取消链接时解析依赖项的方式。
Using require('react')
and require('React')
inconsistently causes this problem as well, even if you only have one version of React. 使用require('react')
和require('React')
不一致也会导致这个问题,即使你只有一个版本的React。
https://www.npmjs.com/package/gulp-browserify didn't have this problem. https://www.npmjs.com/package/gulp-browserify没有这个问题。 Once I stopped using gulp-browserify and switched to watchify+browserify, Uncaught TypeError: Cannot read property 'firstChild' of undefined
started happening. 一旦我停止使用gulp-browserify并切换到watchify + browserify, Uncaught TypeError: Cannot read property 'firstChild' of undefined
开始发生。
It seems to me peerDependencies is not getting traction. 在我看来,peerDependencies并没有得到牵引力。 See https://github.com/npm/npm/issues/5080#issuecomment-40545599 请参阅https://github.com/npm/npm/issues/5080#issuecomment-40545599
I am maintaining react-date-picker (and other react modules), and what I've done until now is to specify the React dependency using the caret, for example ^0.12.0. 我正在维护react-date-picker (以及其他反应模块),到目前为止我所做的是使用插入符号指定React依赖项,例如^ 0.12.0。
Also, when doing a build will all concatenated files, for use outside of the npm ecosystem, I use webpack with externals: { 'react': 'React'}
which will look for the global var React
. 此外,在构建所有连接文件时,为了在npm生态系统之外使用,我使用webpack with externals: { 'react': 'React'}
将查找全局var React
。
对我来说,问题是我在react-bootstrap的<NavItem>
中使用了来自react-router的<Link>
。
If you are experiencing this crash while server side rendering and none of these answers are the problem, here's the likely culprit: 如果您在服务器端呈现时遇到此崩溃而这些答案都不是问题,那么可能是罪魁祸首:
Doing something async (setTimeout, AJAX, etc.) in componentWillMount. 在componentWillMount中执行异步(setTimeout,AJAX等)。 Since componentWillMount is called on the server, this setTimeout/request will still fire. 由于在服务器上调用了componentWillMount,因此仍会触发此setTimeout / request。 If you setState inside this callback then it'll cause the error above. 如果你在这个回调中setState,那么它将导致上面的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.