使用 React 时出现类型错误:无法读取未定义的属性“firstChild" [英] TypeError when using React: Cannot read property 'firstChild' of undefined

查看:24
本文介绍了使用 React 时出现类型错误:无法读取未定义的属性“firstChild"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时,在使用 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//node_modules/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.

两种不同的 React 不会很好地协同工作(至少目前如此).

Two different Reacts won't play nicely together (at least yet).

要修复它,只需删除 node_modules//node_modules/react.
如果您看到一个库将 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆