ReactJS: Uncaught TypeError: Cannot read property of undefined

Problem

You get: Uncaught TypeError: Cannot read property of undefined

This is due to react trying to render components that rely on variables that haven’t yet been set (quite a common occurence).

Solution

You can use conditional rendering shortcut to get rid of this annoyance; this is possible due to react treating undefined as false; so you can then add the following:

{this.props.MyComponentsVar && <MyComponent />}

p.s. this will only be rendered if this.props.MyComponentsVar is defined

https://facebook.github.io/react/docs/jsx-in-depth.html

 

 

keywords: react

About Neil Rubens

see http://ActiveIntelligence.org
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


*