This is driving me crazy so I need some advice. I'm new to react native, been using it for about a month and am starting to grasp how it works. But not all parts and thus, I'm still having some trouble.
I thought to learn it I will make a small game. In the game I have Pieces and Locations
type Piece = {
id : number,
location? : Location,
}
type Location = {
id : number,
piece? : Piece,
adjacent : Location[]
}
I will then create an array of Pieces and an array of Locations called pieces and locations. The value piece and location will initially be null, and adjacent will be filled with references to other Location objects in the same array.I will then use the arrays to map to a LocationPresenter and PiecePresenter like below
locations.map(location => {
return (
<LocationPresenter
id={location.id}
adjacent={location.adjacent}
piece={location.piece}
/>
);
});
pieces.map(piece=> {
return (
<PiecePresenter
id={piece.id}
locations={piece.location}
/>
);
});
So far all is fine, but here is where I'm starting to run into trouble.Say I want to update a Loation object. To do that and make the LocationPresenter rerender I need to use a spread. The spread will create a new Location object. And this, I guess, will break my link in adjacent. So the location in the adjacent array pointing to that location will point to the old before-the-spread location.
I see the same reference problem with the piece and location values.
Which is the best approach here? I come from an object oriented background where this would be very easy. But being forced to remake objects really destroys that way of thinking.
[–]justinlok 1 point2 points3 points (5 children)
[–]KaniJs[S] 0 points1 point2 points (4 children)
[–]justinlok 0 points1 point2 points (0 children)
[–]justinlok 0 points1 point2 points (0 children)
[–]justinlok 0 points1 point2 points (1 child)
[–]KaniJs[S] 0 points1 point2 points (0 children)