reactjs / reactjs/react.dev

[Beta] Using the structuredClone() for updating a nested object

オープン
#5,597 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
JavaScript
スター
11.8k
フォーク
7.9k
平均マージ
1日 11時間
マージ済み PR(30日)
11

説明

https://beta.reactjs.org/learn/updating-objects-in-state#updating-a-nested-object

I think that deep cloning object with structuredClone() and putting the clone into setPerson() is better solution for the problem than using the spread operator.

The the example that was provided can easily be changed this way:

personCopy = structuredClone(person);
personCopy.artwork.city = "New Delhi";
setPerson(personCopy);

This can help avoiding the mistake when spread operator is used to deep clone objects. See example below:

const original = { a: { b: 1 } };
const falseCopy = { ...original };

falseCopy.a.b = 2;

console.log(falseCopy); // logs {a: {b: 2}}
console.log(original); // also logs {a: {b: 2}}

I am sorry if I have failed to follow contributing guidelines. I am aware that the second example is not the best representation of my point.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

React ドキュメントのリンク先のセクション「Updating a nested object」から始め、現在のスプレッド演算子の例を structuredClone() の提案と併せて確認してください。セクションが選択した更新方法を明確に示し、浅いコピーと深いコピーについて読者に誤解を与えないようになれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript, react
領域
documentation
issue の種類
ドキュメント
難易度
2/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
38/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。