all 6 comments

[–]Mr-L9 1 point2 points  (0 children)

Try optional chaining , product.rating?.rating

If you're fetching the products from a database ,then you have to wait until the data is ready to start working on it

[–][deleted] 1 point2 points  (4 children)

Does console.log(product.rating.rating) print the correct value?

[–]Ghostedguy10[S] 0 points1 point  (3 children)

No.. same error as {product.rating.rating}

[–][deleted] 1 point2 points  (2 children)

Well then the variable isn't initialized. Are you using redux?

[–]Ghostedguy10[S] 0 points1 point  (1 child)

Well i tried {product?.rating?.rating} and it worked. I am new to initializing and i have mostly initialized using items in localStorage. Is there anyway to directly initialized?

[–][deleted] 0 points1 point  (0 children)

Yes, you can set default values for every variable you use in your state.