使用 React 时出现类型错误:无法读取未定义的属性“firstChild" [英] TypeError when using React: Cannot read property 'firstChild' of undefined
问题描述
有时,在使用 React 库时,例如 react-router,我会收到此错误:
Sometimes, when using React libraries, such as react-router, I get this error:
未捕获的类型错误:无法读取未定义的属性firstChild"
/~/react-router/~/react/lib/ReactMount.js?:606
Uncaught TypeError: Cannot read property 'firstChild' of undefined
/~/react-router/~/react/lib/ReactMount.js?:606
我该如何解决?
推荐答案
此错误通常由 引起两个版本的 React 一起加载.
例如,如果您 npm install
一个需要不同 React 版本的包并将其放入 dependencies
而不是 peerDependencies
,它可能将单独的 React 安装到 node_modules/
.
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
.
两种不同的 React 不会很好地协同工作(至少目前如此).
Two different Reacts won't play nicely together (at least yet).
要修复它,只需删除 node_modules/
.
如果您看到一个库将 React 放在 dependencies
而不是 peerDependencies
,请提交问题.
To fix it, just delete 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 时出现类型错误:无法读取未定义的属性“firstChild"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!