使用React时的TypeError:无法读取未定义的属性“firstChild” [英] TypeError when using React: Cannot read property 'firstChild' of undefined

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

问题描述

有时,当使用React库时,例如 react-router ,我收到此错误:

Sometimes, when using React libraries, such as react-router, I get this error:


Uncaught TypeError:无法读取未定义的属性'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版本并将其放入<的软件包code> dependencies 而不是 peerDependencies ,它可能会将一个单独的React安装到 node_modules /< some library using React> / 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 /<某些库使用React> / node_modules / react

如果您看到一个库将React放入依赖项而不是 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时的TypeError:无法读取未定义的属性“firstChild”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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