編集:私はこのことを明確にするためにこの質問を書き直しました。これまでの回答に感謝してくれた人々に感謝します。Reactの入れ子状態を管理するための良いアプローチ
複雑なネストされた状態をReactで管理する方法を理解し、コンテンツが変更されていないコンポーネントに対してrender()
が呼び出される回数を制限しようとしています。背景として
:
私はこのようなオブジェクトの「著者」及び「資料」の両方で状態を有すると仮定:
{
'authors' : {
234 : {
'name' : 'Alice Ames',
'bio' : 'Alice is the author of over ...',
'profile_pic' : 'http://....'
},
794 : {
'name' : 'Bob Blake',
'bio' : 'Hailing from parts unknown, Bob...',
'profile_pic' : 'http://....'
},
...more authors...
},
'publications' : {
539 : {
'title' : 'Short Story Vol. 2',
'author_ids' : [ 234, 999, 220 ]
},
93 : {
'title' : 'Mastering Fly Fishing',
'author_ids' : [ 234 ]
},
...more publications...
}
}
この不自然な例では状態によってアクセスされる、2つの領域を有していますauthors
とpublications
のキー。
authors
キーは、作成者のIDでキー入力されたオブジェクトにつながり、一部の作成者データを含むオブジェクトにつながります。
publications
キーは、出版データのある出版物のIDと著者の配列をキーにしたオブジェクトにつながります。
私の状態は以下の擬似JSXのような子コンポーネントとApp
コンポーネントであると仮定します
...
<App>
<AuthorList authors={this.state.authors} />
<PublicationList authors={this.state.authors} publications={this.state.publications} />
</App>
...
...
class AuthorList extends React.Component {
render() {
let authors = this.props.authors;
return (
<div>
{ Object.keys(authors).map((author_id) => {
return <Author author={authors[author_id]} />;
}
</div>
);
}
}
...
...
class PublicationList extends React.Component {
render() {
let publications = this.props.publications;
let authors = this.props.authors;
return (
<div>
{ Object.keys(publications).map((publication_id) => {
return <Publication publication={publications[publication_id]} authors=authors />;
}
</div>
);
}
}
...
はレンダリングPublication
コンポーネントをAuthorList
が子供の束Author
要素を持っている、とPublicationList
は子供の束を持っていると仮定それらの物の実際の内容。
は、ここに私の質問です:私は与えられた作者のためにbio
を更新するとしますが、私はrender()
が、内容に変更されていないすべてAuthor
とPublication
オブジェクトに対して呼び出される必要はありません。この回答から
:
ReactJS - Does render get called any time "setState" is called?
Aは、コンポーネントのrender()
機能は、任意の時点の状態と呼ばれます反応して、またはその両親のいずれかの状態変化 - に関係なく、その状態変化がに何を持っているかどうかにそのコンポーネントの小道具で行う。この動作はshouldComponentUpdateで変更できます。
人々は上記のように複雑な状態をどのように処理するのですか?すべての状態変更で多数のコンポーネントに対してrender()を呼び出すのは良い解決策です(たとえ結果のレンダリングされたオブジェクトが同じで、実際のDOMに発生します)。
うーん...私が言ったことと実質的に同じであるあなたの質問に答えを出す理由は分かりません。 – jpdelatorre
これら2つの答えは事実上同じではないためです。 Author.List内の何かの状態が変わるたびにAuthorListのすべてのAuthorの子が再レンダリングされます。 Mineは、私がやろうとしていたコンテンツを変更したAuthorListの子のみを選択的に再レンダリングします。 – deadcode