I've got the following small example and I assume that if I do a shallow copy before replacing state, that my sessions attribute will not be new, but just a reference to old. I've got the shallow copy defined as the variable newSpeakersDataShallow and the deep copy variable named newSpeakersDataDeep.
If you change what is passed into setSpeakerData, you'll see they both work the same. My question is, what are my risks? if I know that I will not be changing the sessions array, is it OK to use the shallow copy?
(This is just a simplified example so the answer isn't, might as well use deep since it's so easy to do in this case).
https://codesandbox.io/s/clever-varahamihira-ry9x4?file=/pages/index.js:0-1990
Same code as codesandbox:
import { useEffect, useState } from "react";
const data = [
{
id: "1",
first: "Joe",
last: "Smith",
favorite: true,
sessions: [
{
id: "32",
title: "Rails powered by",
},
{
id: "58",
title: "Hello World to .NET 3.5 interoperable Web service",
},
],
},
{
id: "2",
first: "Jon",
last: "Jones",
favorite: false,
sessions: [
{
id: "1011",
title: "scalability and deployability",
},
],
},
{
id: "3",
first: "Sam",
last: "Hulk",
favorite: true,
sessions: [],
},
];
function Speakers() {
const [speakersData, setSpeakersData] = useState([]);
useEffect(() => {
setSpeakersData(data);
}, []);
return (
<>
{speakersData.map(function (speaker) {
return (
<div key={speaker.id}>
<button
onClick={() => {
const newSpeakersDataShallow = speakersData.map(function (rec) {
return speaker.id == rec.id
? { ...rec, favorite: !rec.favorite }
: { ...rec };
});
const newSpeakersDataDeep = speakersData.map(function (rec) {
return speaker.id == rec.id
? {
...rec,
favorite: !rec.favorite,
sessions: [...rec.sessions],
}
: { ...rec };
});
// RESULTS ARE SAME WHETHER DEEP OR SHALLOW COPY USED
setSpeakersData(newSpeakersDataDeep);
}}
>
Toggle Favorite
</button>
{speaker.id} {speaker.first} {speaker.last}{" "}
{JSON.stringify(speaker.sessions)}{" "}
{speaker.favorite === true ? "true" : "false"}
</div>
);
})}
</>
);
}
export default Speakers;