State management in ReGraph relies on immutability, a concept where a prop cannot be changed (mutated) directly once it's created, but it can be updated by replacing it with a new prop. On every render, ReGraph uses referential equality to decide whether any props have changed.
Whenever a new object is passed as a prop, ReGraph assumes something has changed and performs a re-render. If the same object is passed, ReGraph assumes nothing has changed. This is why ReGraph can quickly and efficiently respond to re-renders even in very large datasets.
To prevent unnecessary re-renders, applications must be disciplined in how props are updated. For example, passing a new object with identical property values into the layout prop will trigger a re-render that may change the positions of nodes.
If you want to change a sub-property on a prop that's an object (e.g. the items prop or others), you need to create a whole new object to make sure that ReGraph performs a re-render.
Consider the following object in the items prop:
items: {
n1: {
label: {
color: "blue", // <-- User changes a node's label color
text: "node 1",
},
},
n2: {
label: {
text: "node 2",
},
},
},Let's say we change the n1's label.color property. If we simply mutate the n1 object and pass it into the items prop, the change won't be recognized and our chart will stay the same.
To correctly render the change, we have to create new objects for items and n1, and set a new value for color. You can see the properties that need to be re-created marked with a star below:
items*: {
n1*: {
label*: {
color*: 'red',
text: 'node1'
}
},
n2: {
label: {
text: 'node 2'
}
}
}Deep cloning creates a copy of all the object values, and all the values of all the nested objects within it. In comparison, shallow cloning would create copies of primitive values, but objects wouldn't be copied, only referenced, which would not be recognized during a re-render in ReGraph.
Let's use the example above to illustrate the difference between the two cloning techniques. You may choose to use object destructuring to create a new object that you can modify and pass back to ReGraph:
const shallowClone = { ...items };
shallowClone.a.color = "red";
setItems(shallowClone); // doesn't workThis doesn't work as shallowClone.a equals items.a. The shallowClone.a is just a reference to items.a and ReGraph decides not to do a re-render as, apparently, nothing has changed.
This is what you would need to do instead:
const deepClone = window.structuredClone(items);
deepClone.a.color = "red";
setItems(deepClone); // worksThis does work as deepClone.a does not equal items.a and ReGraph will re-render. See this playground that shows both techniques.
Using object destructuring is possible, but you need to make sure that you don't accidentally pass any references to old objects. Here is an example of how to successfully do this:
// Clone the items object before making any changes
const newItems = Object.assign({}, this.state.items);
// Deep clone and update the item you want to change.
// If you're using spread operator, access nested properties
// to deep clone nested changes as well
const newItem = Object.assign({}, newItems.n1, {
label: {
...newItems.n1.label,
color: "red",
},
});
// Write the change back to the newItems object
newItems.n1 = newItem;
// Update app state and trigger a re-render
this.setState({ items: newItems });Using deep cloning and immutability in your application code provides better performance, results in fewer unexpected side-effects, and lets you rewind or undo user actions. We use these patterns throughout our examples.