TypeError when using React: Cannot read property 'firstChild' of undefined

后端 未结 6 1208
误落风尘
误落风尘 2020-12-29 03:22

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

Uncaught TypeError: Cannot read property \'firstChild\' of undefined

6条回答
  •  没有蜡笔的小新
    2020-12-29 04:17

    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. Since componentWillMount is called on the server, this setTimeout/request will still fire. If you setState inside this callback then it'll cause the error above.

提交回复
热议问题