First of all, I'm a newbie at frontend, so my knowledge about javascript + react is based on tutorials.
I have an object that is set to the component properties. This object has an array as a property.
So inside the component, either in a function or the render method, if a write a console.log(object.property)
It prints the array, no error.
But if I try to use the .includes method
console.log(object.property.includes("string"))
it throws the following error:
react-dom.development.js?cada:12404 Uncaught TypeError: Cannot read property 'includes' of undefined
so how does the array turn into undefined just for using this method?
The workaround I found, is something like this:
render() {
let self = this;
let user;
return(
{
this.props.otherarray.map(function(var) {
self.user = self.props.object.property.includes("string")
})
console.log(this.user)
}
);
}
and then it works.
What am I missing? Is it something about javascript context or react stuff?
map()needs areturnor get array of allundefinedvalues and you usually use thevarto do something in the map callbackincludesreturns true or falseobjectprop coming from an async operation?self.props.object.propertyis not an array