Updating Arrays in State – React
6
公开标注数
6
参与人数
2026-07-24 09:44:56
首次 Whisper
本页的公开 Whisper
划选高亮2026-07-24 16:08:56
原文高亮摘录
“Unfortunately, slice and splice are named similarly but are very different”
Whisper 随想笔记
Every single time I mix those up, it's a bug hunt.
划选高亮2026-07-24 13:05:56
原文高亮摘录
“every time you want to update an array, you’ll want to pass a new array to your state setting function.”
Whisper 随想笔记
But what if the array is huge? Copying feels wasteful sometimes.
划选高亮2026-07-24 12:56:56
原文高亮摘录
“every time you want to update an array, you’ll want to pass a new array to your state setting function.”
Whisper 随想笔记
So basically never mutate, just copy and replace. Got it.
划选高亮2026-07-24 10:02:56
原文高亮摘录
“Arrays are mutable in JavaScript, but you should treat them as immutable when you store them in state.”
Whisper 随想笔记
I mean, it's just a guideline, but React's whole re-render thing kind of forces you to do it.
划选高亮2026-07-24 09:53:56
原文高亮摘录
“Arrays are mutable in JavaScript, but you should treat them as immutable when you store them in state.”
Whisper 随想笔记
So true, I kept mutating state directly and my components wouldn't re-render. Learned that the hard way.
划选高亮2026-07-24 09:44:56
原文高亮摘录
“Arrays are mutable in JavaScript, but you should treat them as immutable when you store them in state.”
Whisper 随想笔记
Yeah but copying arrays every time feels so wasteful, especially with big lists.
分享本页 Whisper
短链接
https://domwhisper.com/s/780ad74e1370嵌入代码
<iframe src="https://domwhisper.com/embed/780ad74e1370" width="100%" height="480" style="border:0;border-radius:16px" loading="lazy"></iframe>