Skip to content
Advertisement

React: Props sent by parent are not the same received by child

Basically the title sums it all, I’m sending a prop to a child and comes different. I make a console.log just before rendering of parent and the prop is OK and make a console.log of same prop at the beginning of Child component and it’s different.

This is the parent:

JavaScript

And this is the child (I removed mostly everything from child because after receiving the prop wrong everything else is wrong, when I use a mock prop it works):

JavaScript

export default Gameboard;

I have a suspicion it has something to do with the randomPlaceShip method in parent, because what I receive in child is an array different from the parent one but as if it had its own randomPlaceShip (with another result). The randomePlaceShip method is the following:

JavaScript

The console.log within the method matches on what I get in parent; however, in the child component which apparently gets another go to the method won’t show me that console.log so I’m not sure if it’s really running the method.

Advertisement

Answer

I managed to solve it, as Linda also commented, by using setState.

This way:

JavaScript

In this case the function fillBoard contains the function randomePlaceShip done to all of the five ships required to play.

Advertisement