Skip to Content
流暢なReact
book

流暢なReact

by Tejas Kumar
March 2025
Beginner to intermediate
336 pages
5h 1m
Japanese
O'Reilly Media, Inc.
Audio summary available
Content preview from 流暢なReact

第3章 仮想DOM 仮想DOM

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

この章では、仮想DOM (vDOMと呼ばれることもある)の概念と、Reactにおけるその意義について深く掘り下げていく。また、Reactが仮想DOMをどのように利用し、ウェブ開発をより簡単かつ効率的にするのかについても探っていく。

ウェブ・アプリケーションが複雑になるにつれて、「本物のDOM」を管理するのはますます難しくなってくる。Reactの仮想DOMは、この問題に対する解決策を提供する。

この章では、Reactの仮想DOMの仕組み、実DOMに対する利点、実装方法について説明する。また、Reactが仮想DOMを使って実DOMのパフォーマンスを最適化する方法と、それがどのように組み合わされるのかについても説明する。

一連のコード例と詳細な説明を通して、Reactにおける仮想DOMの役割と、堅牢性と効率性に優れたWebアプリケーションを作成するためにその利点を活用する方法を理解する。さっそく始めよう!

バーチャルDOM入門

仮想DOMは、DOMと同様、JavaScriptオブジェクトとしてモデル化されたHTML文書である。これは文字通り、DOM(Document Object Model)の意味するところである。DOMそのものが、ブラウザ・ランタイムのドキュメント・モデルである。バーチャルDOMはこれを軽量にコピーしたもので、実際のDOMがNode オブジェクト で構成されているのに対し、バーチャルDOMはプレーンなJS オブジェクトで構成され、記述として機能するという違いがある。この章で説明するように、Web開発者はより効率的でパフォーマンスの高い方法でユーザインタフェースを作成することができる。

Reactでは、setState やその他のメカニズムによってUIに変更を加えるよう指示すると、まず仮想DOMが更新され、次に仮想DOMの変更に合わせて実DOMが更新される。このプロセスはリコンシリエーションと呼ばれ、第4章の主題となる。

仮想DOMを最初に更新する理由は、実DOMの更新にやや時間がかかり、コストがかかるからだ 。これについては次のセクションで説明するが、要するに、実DOMに変更が加えられるたびに、ブラウザはページのレイアウトを再計算したり、画面を再描画したり、時間のかかるその他の演算子を実行しなければならないのだ。

例えば、 要素のoffsetWidth を読み取るだけで、ブラウザがドキュメントの全部または一部のレイアウトを再計算する処理であるリフローを引き起こす可能性があり、パフォーマンスに影響を与えたり、DOMとの直接的なインタラクションの効率が悪くなる可能性がある:

const btn = document.getElementById("myButton");
const width = btn.offsetWidth; // This can trigger a reflow

一方、仮想DOMの更新は、実際のページレイアウトの変更を伴わないため、はるかに高速だ。その代わりに、ブラウザや他のホスト環境から切り離され、利用可能なJavaScriptエンジンを最大限に活用し、時間とともにその効率を高めることができる様々なアルゴリズム的アプローチによって、迅速かつ効率的に操作することができるシンプルなJavaScriptオブジェクトである。

仮想DOM に更新が加えられると、Reactは差分アルゴリズム ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

ウェブ・アクセシビリティ・クックブック

ウェブ・アクセシビリティ・クックブック

Manuel Matuzovic

Publisher Resources

ISBN: 9798341624689