Skip to Content
JavaScriptデザインパターンを学ぶ 第2版
book

JavaScriptデザインパターンを学ぶ 第2版

by Addy Osmani
March 2025
Intermediate to advanced
298 pages
4h 18m
Japanese
O'Reilly Media, Inc.
Content preview from JavaScriptデザインパターンを学ぶ 第2版

第14章. React.jsのアプリケーション構造

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

小さな趣味のプロジェクト( )を作ったり、新しい概念やライブラリを試したりするとき、開発者は計画や整理構造を持たずに、フォルダにファイルを追加し始めることがある。これらには、CSS、ヘルパーコンポーネント、画像、ページなどが含まれる。プロジェクトが大きくなるにつれて、すべてのリソースを一つのフォルダで管理することは不可能になる。それなりの規模のコードベースであれば、論理的な基準に基づいてアプリケーションのフォルダ構造に整理する必要がある。ファイルやアプリケーションコンポーネントをどのように構造化するかは、個人的/チーム的な選択となる。また、一般的には、アプリケーションのドメインと使用する技術に依存するだろう。

この章では、主にReact.jsアプリケーションのフォルダ構造に焦点を当て、プロジェクトが成長するにつれて、より良い管理ができるようにする。

はじめに

React.js自体は、 、プロジェクトの構造化に関するガイドラインを提供していないが、よく使われるアプローチをいくつか提案している。複雑なプロジェクトやNext.jsアプリケーションのためのフォルダ構造について議論する前に、これらを見て、その長所と短所を理解しよう。

高レベルでは、Reactアプリケーションのファイルを2つの方法でグループ化できる:

機能別にグループ化する

アプリケーションモジュール、機能、ルートごとにフォルダを作成する。

ファイルタイプでグループ化する

ファイルの種類ごとにフォルダを作成する。

この分類を詳しく見てみよう。

モジュール、フィーチャー、ルートでグループ化する

この場合、ファイル構造は 、ビジネスモデルやアプリケーションフローを反映することになる。たとえば、eコマースアプリケーションがあれば、product、productlist、checkoutなどのフォルダがある。productモジュールに明示的に必要なCSS、JSXコンポーネント、テスト、サブコンポーネント、またはヘルパーライブラリはproductフォルダに存在する:

common/
  Avatar.js
  Avatar.css
  ErrorUtils.js
  ErrorUtils.test.js
product/
  index.js
  product.css
  price.js
  product.test.js
checkout/
  index.js
  checkout.css
  checkout.test.js

機能ごとにファイルをグループ化する利点は、モジュールに変更があった場合、影響を受けるすべてのファイルが同じフォルダにまとめられ、コードの特定の部分に変更が局所化されることだ。

欠点は、モジュール間で使用される共通のコンポーネント、ロジック、スタイルを定期的に特定し、繰り返しを避け、一貫性と再利用を促進しなければならないことである。

ファイルタイプでグループ化する

このタイプのグループ化では、 、CSS、コンポーネント、テストファイル、イメージ、ライブラリなどのために異なるフォルダを作成することになる。このように、論理的に関連するファイルは、ファイルタイプに基づいて異なるフォルダに存在することになる:

css/ global.css checkout.css product.css lib/ date.js currency.js ...
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

アルゴリズムクイックリファレンス 第2版

アルゴリズムクイックリファレンス 第2版

George T. Heineman, Gary Pollice, Stanley Selkow, 黒川 利明, 黒川 洋
Node.jsデザインパターン 第2版

Node.jsデザインパターン 第2版

Mario Casciaro, Luciano Mammino, 武舎 広幸, 阿部 和也

Publisher Resources

ISBN: 9798341625952