Here's the basic idea...
constructor(props) {
super(props);
this.state = {
children: [{id:1},{id:2}], // HERE ARE THE TWO OBJECTS
style: {top: 0}
};
}
Now let's say I update one of those two objects but those objects are mapped to an array of components.
<div className="thread">
{this.state.children.map((child) =>
<Post
key={child.id}
id={child.id}
/>
)}
</div>
Now... when I update one of those objects...
changeID (id, newID) { // this is a different function, just an example
let temp = this.state.children;
for (let i = 0; i < temp.length; i++) {
if (temp[i].id === id) {
temp[i].id = newID; // <- update this
}
}
this.setState({
children: temp // <- plug temp[] back as the updated state
});
}
I throw the new state back in, but it updates each of the mapped objects.
// on one change to one element
id 1 POST UPDATED
id 2 POST UPDATED
1) Does it re-render EVERY component (in the mapped array) or is it smart enough to tell that the state values passed as props to the mapped component are the same?
2) Is it incredibly expensive in processing power if that array is significantly longer?
3) If yes, how can I go about this better? Constructive criticism is appreciated.
Thank you!