Why are you trying to get into this.props.location.state? Forget that, the information you need is in this.props.shops, no? Also, im pretty sure this.props.location is a property of react-router; and it works for getting the pathname, and you already have it in the id variable.
If you see that shop is undefined; but there is information in you variable data (where you are getting the information from); then the problem is your filtering. Check it is filtering correctly.
since this.props.location.shops is not a valid property on the object.
I wouldn’t recommend storing state in window.location, which is what you’ve done here. Instead, just access shops directly from this.state or this.props.
This is because this.props.location is a reference to window.location, so you’re actually mutating the global location property.
Edit:
I see you’ve already accessed your data from this.props. I’d still recommend you also grab the location from this.props rather than window.location.pathname.replace("/shopDetail/", "")
The error says ‘Cannot read property map of undefined’. Look at the error trace, and find the highlighted line. It will have a ‘^’ where the error starts. Then look to see why there is no map method on that object.
To render my shop data, should not be done with map since I cannot access the object keys with it. I should just render it with shop.nom etc.
But when I try to render it with {shop.nom}, I have an error return saying TypeError: Cannot read property ‘nom’ of undefined What is weird because I have an object with this “nom” key inside as you can see in my last screen shot edit
But when I try to render it with {shop.nom}, I have an error return saying TypeError: Cannot read property ‘nom’ of undefined
Are you sure it’s definitely returning something? array.find returns undefined if it does not find a match. In javascript, you have to make sure that each level in an object exists if you want to get it. That means if you have
const obj = {
a: {
b: {
c: 10
}
}
}
and you want to get the value at c, you have to do
My point is may be an error from component life cycle . But yes, I will try to verify what you are saying too.
I’ll let you know quickly. Thanks anyway for your time !
It is because at the moment the component renders, there is not value inside shop.
You can know that by looking at that icon.
You have 2 options:
Put all your information in state, not like shop={}, but like bornePhoto, cabinePhoto, helio, booth… etc, then it first renders with empty information, and then when the information arrives, it updates the state and it will shown.
Conditional render. Example:
if (!shop.nom){
return null
} else {
return <h1>{shop.nom}<h1/>
}
OR YOU CAN DO:
{Boolean(shop.nom) ? <h1>{shop.nom}<h1/> : null}
Here we go, Thanks a lot to both of you.
Here its render the right shop, with the right information.
So, a last question, that wanna say these kind of error are caused by my way to code? it could be fix with a better life cycle handling ? Or its just normal to have to do this way to render this kind of stuff ?
Once again, Thanks u very much for your time, I really appreciated it.
The best practice it to store your data in state, and render depending on the value of that. You will still need to handle if the data is missing. In fact, you should always handle a missing data case.
So in componentDidMount you would call out to the database, while the component renders your default state.
Then when the data comes in, update your state and the component will re-render itself. And the best practice is to render null as a component, which won’t add any nodes to your dom.
So this particular error wasn’t about how you were writing the logic (except for not handling the missing data case).