I want to do some conditional rendering based on what is in state. My state looks something along the lines of this:
state = {
text: '',
properties: {
someMoreData: [{...}]
},
moreData: ''
}
I want to do conditional rendering based on whether or not the someMoreData field exists within the properties key in my state. My current code looks like this:
render () {
console.log(this.state)
if('someMoreData' in this.state.properties)
return (
<div>
..
</div>
)
}
React is throwing an error stating Cannot use 'in' operator to search for 'someMoreData' in undefined
How to can i format my code so that if the someMoreData field exists, I get my JSX, and if not i render some other JSX.
inoperator works nor how it is used in JavaScript. If you simply need to know whether or notsomeMoreDatais not falsy, you can doif (this.state.properties.someMoreData) { ... some code ... }.