ReactでContextの値を動的に更新する方法と実装例

ReactのContext APIは、コンポーネントツリー全体で値を共有し、Props Drilling(プロップスの受け渡しの連鎖)を回避するための強力なツールです。しかし、Contextの値を動的に更新する方法を正しく理解していないと、コードが複雑化し、予期しないバグを引き起こすことがあります。本記事では、Contextの基本的な使い方から始め、値を動的に更新する具体的な実装方法と、実際のプロジェクトで応用できるアイデアについて詳しく解説します。これにより、Reactアプリケーションの状態管理がさらに柔軟で効率的になります。

目次
  1. Context APIとは
    1. Context APIの役割
    2. 基本的な仕組み
  2. Contextを利用する際の課題
    1. 1. 再レンダリングの過剰発生
    2. 2. コンテキストの粒度の設計
    3. 3. デバッグの難しさ
    4. 4. Typescriptとの統合
    5. まとめ
  3. Contextの値を動的に更新するための準備
    1. 1. Contextの作成
    2. 2. Providerの作成
    3. 3. アプリケーションにProviderを組み込む
    4. 4. Contextの利用準備が完了
  4. 実装例: 状態を動的に変更するContextの作成
    1. 1. Contextの作成
    2. 2. Providerコンポーネントの作成
    3. 3. 値を使用するコンポーネント
    4. 4. アプリケーション全体でContextを使用
    5. 実行結果
    6. ポイント
  5. Contextの値を更新する仕組み
    1. 1. `useState`を利用した値の更新
    2. 2. `useReducer`を利用した値の更新
    3. 3. 値の消費と更新
    4. 4. 状態管理フックの選択基準
  6. Contextの値を消費するコンポーネントの作成
    1. 1. Contextの値を使用する基本的なコンポーネント
    2. 2. 複数の値を使用するコンポーネント
    3. 3. `Context.Consumer`を利用した例
    4. 4. コンポーネント分割の推奨
    5. まとめ
  7. Context値の動的更新時の注意点
    1. 1. 再レンダリングの最適化
    2. 2. 冗長な再レンダリングの防止
    3. 3. コンポーネントの階層化と依存関係
    4. 4. 非同期処理の管理
    5. 5. デバッグのしやすさ
    6. 6. Typescriptの導入
    7. まとめ
  8. 演習: 複数の値を管理する動的Contextの構築
    1. 1. 要件
    2. 2. Contextの作成
    3. 3. 値を消費するコンポーネントの作成
    4. 4. アプリケーションの構築
    5. 5. 演習内容
    6. 6. 結果と応用
  9. まとめ

Context APIとは


ReactのContext APIは、React 16.3で導入された機能で、アプリケーション内でデータをコンポーネントツリー全体に渡すための仕組みです。通常、親から子へプロップスを介してデータを渡しますが、ツリーが深くなると、すべての中間コンポーネントを経由させる必要があります。このような状況を「Props Drilling」と呼び、コードの保守性が低下する原因となります。

Context APIの役割


Context APIは、以下のようなシナリオで使用されます:

  • ユーザー認証情報の管理
  • テーマの切り替え(ライトモードとダークモードなど)
  • 言語設定(i18nの実装)
    これらのグローバルデータを管理することで、複数のコンポーネント間で効率的に値を共有できます。

基本的な仕組み


Context APIは主に以下の3つの要素で構成されます:

  1. React.createContext: 新しいContextを作成します。
  2. Provider: コンポーネントツリーに値を供給する役割を果たします。
  3. ConsumerまたはuseContext: Contextの値を取得して使用するための方法を提供します。

以下は、Contextの基本的な使用例です:

import React, { createContext, useContext } from 'react';

const MyContext = createContext();

function App() {
  return (
    <MyContext.Provider value="Hello, Context!">
      <ChildComponent />
    </MyContext.Provider>
  );
}

function ChildComponent() {
  const value = useContext(MyContext);
  return <p>{value}</p>;
}

このように、Context APIを利用すると、データの受け渡しが効率的になり、コードが簡潔になります。

Contextを利用する際の課題

ReactのContextは便利な状態管理のツールですが、適切に使用しないとパフォーマンスやメンテナンス性の面で問題を引き起こすことがあります。ここでは、Context使用時に直面しやすい主な課題について説明します。

1. 再レンダリングの過剰発生


Contextの値が更新されると、それを利用しているすべてのコンポーネントが再レンダリングされます。これにより、不要なレンダリングが発生し、アプリケーションのパフォーマンスが低下する可能性があります。

例: 再レンダリングの問題


以下のコードでは、UserContext内の値が更新されると、値を消費していないコンポーネントも再レンダリングされます。

const UserContext = createContext();

function App() {
  const [user, setUser] = useState({ name: "John" });

  return (
    <UserContext.Provider value={user}>
      <Header />
      <MainContent />
    </UserContext.Provider>
  );
}

2. コンテキストの粒度の設計


1つのContextに多くのデータや機能を詰め込むと、データの変更が不要な箇所にも影響を与えることがあります。Contextの粒度を適切に設計しないと、複雑なコードになりがちです。

改善策


データごとに複数のContextを作成し、必要な箇所でのみ利用することで、影響範囲を限定します。

const UserContext = createContext();
const ThemeContext = createContext();

3. デバッグの難しさ


Contextを多用すると、どのProviderが値を供給しているのか、どの値がどのタイミングで更新されたのかを追跡するのが難しくなることがあります。

解決策

  • React Developer Toolsを利用してContextの値を可視化する。
  • 状態管理ツール(例: ReduxやZustand)と組み合わせて使用する。

4. Typescriptとの統合


Typescriptを使用する場合、Contextの型定義が複雑になることがあります。これにより、開発の初期段階でエラーが増える可能性があります。

改善策


適切に型定義を行い、Contextのプロバイダーやコンシューマーを型安全に利用します。

interface User {
  name: string;
  age: number;
}

const UserContext = createContext<User | undefined>(undefined);

まとめ


Contextを使用する際には、再レンダリングの制御や粒度の設計、デバッグの容易さに配慮する必要があります。これらの課題を意識して実装することで、Contextの利便性を最大限に活かすことができます。

Contextの値を動的に更新するための準備

Contextの値を動的に更新するためには、状態管理の仕組みと更新関数をProviderで供給する必要があります。以下では、そのための準備手順を解説します。

1. Contextの作成


まず、createContextを使用してContextを作成します。この際、初期値を設定します。初期値には、状態とその更新関数を含めるのが一般的です。

import React, { createContext, useState } from "react";

export const UserContext = createContext({
  user: null,
  setUser: () => {},
});

2. Providerの作成


Contextの値を提供するProviderを作成します。このProviderで状態を管理し、状態の更新関数をContextに渡します。

export function UserProvider({ children }) {
  const [user, setUser] = useState(null);

  return (
    <UserContext.Provider value={{ user, setUser }}>
      {children}
    </UserContext.Provider>
  );
}

この例では、userとその更新関数setUserをContextで供給しています。

3. アプリケーションにProviderを組み込む


アプリケーション全体でContextを使用するために、ルートコンポーネントにProviderを組み込みます。これにより、アプリケーション内のどのコンポーネントからでも値にアクセスできます。

import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import { UserProvider } from "./UserContext";

ReactDOM.render(
  <UserProvider>
    <App />
  </UserProvider>,
  document.getElementById("root")
);

4. Contextの利用準備が完了


以上の手順で、Contextに状態とその更新関数を供給する準備が整いました。次のステップでは、これを利用して、状態を動的に更新する実装例を紹介します。

実装例: 状態を動的に変更するContextの作成

ここでは、動的に値を変更するContextの具体的な実装例を紹介します。この例では、ユーザー情報(名前)を動的に更新する仕組みを構築します。

1. Contextの作成


Contextを作成し、状態と更新関数を初期値として提供します。

import React, { createContext, useState } from "react";

export const UserContext = createContext({
  user: { name: "" },
  updateUser: () => {},
});

2. Providerコンポーネントの作成


Providerを作成し、状態と更新関数をContextに供給します。

export function UserProvider({ children }) {
  const [user, setUser] = useState({ name: "John Doe" });

  const updateUser = (newName) => {
    setUser({ name: newName });
  };

  return (
    <UserContext.Provider value={{ user, updateUser }}>
      {children}
    </UserContext.Provider>
  );
}

このコードでは、userオブジェクトを管理する状態と、その更新関数updateUserをContextとして供給します。

3. 値を使用するコンポーネント


次に、Contextの値を取得し、UIに反映するコンポーネントを作成します。

import React, { useContext } from "react";
import { UserContext } from "./UserContext";

function UserProfile() {
  const { user, updateUser } = useContext(UserContext);

  const handleChangeName = () => {
    const newName = prompt("Enter new name:");
    if (newName) {
      updateUser(newName);
    }
  };

  return (
    <div>
      <h2>User Profile</h2>
      <p>Name: {user.name}</p>
      <button onClick={handleChangeName}>Change Name</button>
    </div>
  );
}

export default UserProfile;

このコードでは、useContextを使用してuserupdateUserを取得し、ボタンをクリックするとユーザー名を変更できる仕組みを提供しています。

4. アプリケーション全体でContextを使用


アプリケーション全体でProviderを利用して、動的に状態を変更できるようにします。

import React from "react";
import ReactDOM from "react-dom";
import { UserProvider } from "./UserContext";
import UserProfile from "./UserProfile";

function App() {
  return (
    <div>
      <h1>Dynamic Context Example</h1>
      <UserProfile />
    </div>
  );
}

ReactDOM.render(
  <UserProvider>
    <App />
  </UserProvider>,
  document.getElementById("root")
);

実行結果


アプリケーションを起動すると、初期状態では「John Doe」と表示されます。「Change Name」ボタンをクリックすると、新しい名前を入力するプロンプトが表示され、入力した名前が動的に反映されます。

ポイント

  • 状態の更新にはsetStateを使用することで、Contextの値を動的に変更できます。
  • 状態管理を簡潔にするため、updateUserのような更新専用の関数を作成するのが推奨されます。

これにより、柔軟で拡張性のあるContextを利用した状態管理が可能になります。

Contextの値を更新する仕組み

ReactのContextを使用して値を動的に更新する際には、状態管理フックであるuseStateuseReducerを利用します。ここでは、それぞれの方法について解説します。

1. `useState`を利用した値の更新

useStateを使用すると、シンプルな状態の管理が可能です。以下の例では、名前を管理し、更新する仕組みを構築します。

import React, { createContext, useState } from "react";

export const UserContext = createContext();

export function UserProvider({ children }) {
  const [user, setUser] = useState({ name: "John Doe" });

  const updateUser = (newName) => {
    setUser({ name: newName });
  };

  return (
    <UserContext.Provider value={{ user, updateUser }}>
      {children}
    </UserContext.Provider>
  );
}

特徴

  • シンプルで直感的な実装が可能。
  • 状態の種類が少ない場合や、単純な値の更新に適しています。

2. `useReducer`を利用した値の更新

状態が複雑な場合や複数の更新操作が必要な場合は、useReducerを使用します。この方法では、状態を定義し、状態を更新するロジック(リデューサー)を明示的に記述します。

import React, { createContext, useReducer } from "react";

export const UserContext = createContext();

const initialState = { name: "John Doe", age: 30 };

function userReducer(state, action) {
  switch (action.type) {
    case "SET_NAME":
      return { ...state, name: action.payload };
    case "SET_AGE":
      return { ...state, age: action.payload };
    default:
      return state;
  }
}

export function UserProvider({ children }) {
  const [state, dispatch] = useReducer(userReducer, initialState);

  const updateName = (newName) => dispatch({ type: "SET_NAME", payload: newName });
  const updateAge = (newAge) => dispatch({ type: "SET_AGE", payload: newAge });

  return (
    <UserContext.Provider value={{ user: state, updateName, updateAge }}>
      {children}
    </UserContext.Provider>
  );
}

特徴

  • 状態が多岐にわたる場合や、更新操作が複数ある場合に適しています。
  • 状態変更のロジックを一元管理でき、可読性が向上します。

3. 値の消費と更新

コンポーネント内で値を取得し、更新を反映します。以下の例は、useReducerで構築されたContextを使用したものです。

import React, { useContext } from "react";
import { UserContext } from "./UserContext";

function UserProfile() {
  const { user, updateName, updateAge } = useContext(UserContext);

  return (
    <div>
      <h2>User Profile</h2>
      <p>Name: {user.name}</p>
      <p>Age: {user.age}</p>
      <button onClick={() => updateName("Jane Doe")}>Change Name</button>
      <button onClick={() => updateAge(35)}>Change Age</button>
    </div>
  );
}

export default UserProfile;

動作

  • 「Change Name」ボタンをクリックすると、名前が「Jane Doe」に変更されます。
  • 「Change Age」ボタンをクリックすると、年齢が「35」に更新されます。

4. 状態管理フックの選択基準

  • useState: シンプルな状態や単一の値を管理したい場合に使用。
  • useReducer: 複雑な状態や複数の更新アクションを扱いたい場合に使用。

これらのフックを活用することで、Contextの値を効率的に管理し、アプリケーションの状態を動的に更新できます。

Contextの値を消費するコンポーネントの作成

ReactのContextで供給された値を使用するには、useContextフックまたはContext.Consumerを使用します。ここでは、実際にContextの値を消費し、それをUIに反映するコンポーネントを作成します。

1. Contextの値を使用する基本的なコンポーネント

以下は、ユーザー情報を取得し、表示するコンポーネントの例です。

import React, { useContext } from "react";
import { UserContext } from "./UserContext";

function UserProfile() {
  const { user, updateUser } = useContext(UserContext);

  const handleChangeName = () => {
    const newName = prompt("Enter a new name:");
    if (newName) {
      updateUser(newName);
    }
  };

  return (
    <div>
      <h2>User Profile</h2>
      <p>Name: {user.name}</p>
      <button onClick={handleChangeName}>Change Name</button>
    </div>
  );
}

export default UserProfile;

コードのポイント

  • useContext(UserContext)を使用してContextの値を取得します。
  • user(状態)とupdateUser(更新関数)を分解して使用します。
  • ボタンをクリックすると、プロンプトで新しい名前を入力し、それを更新する動作を実現します。

2. 複数の値を使用するコンポーネント

複数の状態や関数がContextで供給されている場合、それらを同時に消費できます。

function AdvancedUserProfile() {
  const { user, updateName, updateAge } = useContext(UserContext);

  const handleChangeName = () => {
    const newName = prompt("Enter a new name:");
    if (newName) {
      updateName(newName);
    }
  };

  const handleChangeAge = () => {
    const newAge = prompt("Enter a new age:");
    if (newAge) {
      updateAge(parseInt(newAge, 10));
    }
  };

  return (
    <div>
      <h2>Advanced User Profile</h2>
      <p>Name: {user.name}</p>
      <p>Age: {user.age}</p>
      <button onClick={handleChangeName}>Change Name</button>
      <button onClick={handleChangeAge}>Change Age</button>
    </div>
  );
}

export default AdvancedUserProfile;

特徴

  • 2つのボタンを用意し、それぞれ名前と年齢を動的に変更できるようにしています。
  • 状態が複数ある場合でも、必要に応じて柔軟に操作可能です。

3. `Context.Consumer`を利用した例

React 16.8以前やuseContextを使用しない場合、Context.Consumerを利用して値を取得します。

function UserProfileWithConsumer() {
  return (
    <UserContext.Consumer>
      {({ user, updateUser }) => (
        <div>
          <h2>User Profile</h2>
          <p>Name: {user.name}</p>
          <button onClick={() => updateUser("New Name via Consumer")}>
            Change Name
          </button>
        </div>
      )}
    </UserContext.Consumer>
  );
}

export default UserProfileWithConsumer;

特徴

  • Consumerはレンダープロップスの形式で値を提供します。
  • 新しいフック形式のuseContextに比べてやや冗長ですが、同じ結果が得られます。

4. コンポーネント分割の推奨

  • 表示専用コンポーネント: userなどの状態を表示するためのコンポーネントを分ける。
  • ロジックコンポーネント: ボタンのクリックイベントや状態更新のロジックを処理するコンポーネントを作る。

こうすることで、コードの保守性が向上し、コンポーネントの再利用性も高まります。

まとめ


useContextを活用すれば、Contextの値を簡潔に取得し、動的に操作することが可能です。また、必要に応じてContext.Consumerを使用しても、同様の機能を実現できます。これにより、Reactアプリケーション内で状態管理が効率的に行えます。

Context値の動的更新時の注意点

Contextを動的に更新することでアプリケーションの状態管理が簡潔になりますが、正しく設計しないとパフォーマンスや保守性に悪影響を与える場合があります。以下では、動的更新時の注意点とその対策を解説します。

1. 再レンダリングの最適化

問題点
Contextの値が変更されると、その値を使用しているすべてのコンポーネントが再レンダリングされます。これは、不要なレンダリングを引き起こし、アプリケーションのパフォーマンスに影響を与える可能性があります。

解決策

  • Contextの分割: 1つのContextに多くの値を詰め込むのではなく、用途ごとにContextを分けて影響範囲を限定します。
  • メモ化の活用: 値や関数をuseMemouseCallbackでメモ化して、再生成を防ぎます。

例: Contextの分割

const UserContext = createContext();
const ThemeContext = createContext();

例: メモ化

const value = useMemo(() => ({ user, setUser }), [user]);
<UserContext.Provider value={value}>{children}</UserContext.Provider>

2. 冗長な再レンダリングの防止

問題点
Contextの値を使用していないコンポーネントが再レンダリングされる場合があります。

解決策

  • Contextから値を直接参照するのではなく、必要な部分でのみuseContextを使用する。
  • 特定のコンポーネントに必要な部分だけを渡すための「カスタムフック」を作成する。

例: カスタムフック

export const useUser = () => useContext(UserContext);

3. コンポーネントの階層化と依存関係

問題点
Contextが深い階層に渡されると、設計が複雑になり、デバッグが困難になります。

解決策

  • 適切な粒度でProviderを配置: 必要な部分にのみProviderを適用することで、アプリケーション全体に広がる不要な依存を防ぎます。
  • コンポーネントの構造を簡潔に保つ: 無駄に深いネストを避け、Contextの使用範囲を明確にします。

4. 非同期処理の管理

問題点
非同期関数(API呼び出しなど)でContextの値を更新する場合、値の競合や意図しない状態になることがあります。

解決策

  • 非同期処理の分離: 非同期処理はカスタムフックや専用のロジック層で管理し、Context更新はその結果を基に行う。
  • ローディング状態の管理: 状態更新中のローディング状態を明示することで、ユーザー体験を向上させる。

例: 非同期更新の分離

async function fetchUserData() {
  const response = await fetch("/api/user");
  const data = await response.json();
  setUser(data);
}

5. デバッグのしやすさ

問題点
Contextの値がどのタイミングで更新されたのか、特定が難しい場合があります。

解決策

  • 状態の変更ログを記録するためのデバッグ用ラッパーを導入する。
  • React Developer Toolsを活用してContextの値を追跡する。

例: 状態変更ログ

const updateUser = (newName) => {
  console.log(`User updated: ${newName}`);
  setUser({ name: newName });
};

6. Typescriptの導入

問題点
型が定義されていないと、値の更新時に意図しないエラーが発生する場合があります。

解決策
Contextの型を厳密に定義し、型安全な更新関数を提供します。

例: 型定義

interface User {
  name: string;
  age: number;
}

const UserContext = createContext<{
  user: User;
  updateUser: (user: User) => void;
} | null>(null);

まとめ

  • 再レンダリングを最小限に抑えるために、Contextの分割やメモ化を活用しましょう。
  • 非同期処理やローディング状態を適切に管理することで、意図しない競合を防ぎます。
  • デバッグの容易さを確保し、型定義を利用して開発効率を向上させましょう。

これらの注意点を意識することで、Contextを使用した状態管理がよりスムーズで効率的になります。

演習: 複数の値を管理する動的Contextの構築

ここでは、複数の値(例: ユーザー情報とテーマ設定)をContextで動的に管理する仕組みを構築する演習を紹介します。この応用例を通じて、Contextの設計と実装スキルを向上させましょう。

1. 要件


以下の要件を満たすReactアプリケーションを構築します:

  1. ユーザー情報: 名前と年齢を管理し、動的に更新する。
  2. テーマ設定: ライトテーマとダークテーマを切り替えられるようにする。

2. Contextの作成

2つのContextを作成します。それぞれに状態と更新関数を供給します。

import React, { createContext, useState } from "react";

// User Context
export const UserContext = createContext({
  user: { name: "", age: 0 },
  updateUser: () => {},
});

// Theme Context
export const ThemeContext = createContext({
  theme: "light",
  toggleTheme: () => {},
});

// Providerの作成
export function AppProvider({ children }) {
  const [user, setUser] = useState({ name: "John Doe", age: 25 });
  const [theme, setTheme] = useState("light");

  const updateUser = (newUser) => setUser(newUser);
  const toggleTheme = () => setTheme((prevTheme) => (prevTheme === "light" ? "dark" : "light"));

  return (
    <UserContext.Provider value={{ user, updateUser }}>
      <ThemeContext.Provider value={{ theme, toggleTheme }}>
        {children}
      </ThemeContext.Provider>
    </UserContext.Provider>
  );
}

3. 値を消費するコンポーネントの作成

ユーザー情報とテーマ設定を操作できるコンポーネントを作成します。

import React, { useContext } from "react";
import { UserContext, ThemeContext } from "./AppProvider";

function UserProfile() {
  const { user, updateUser } = useContext(UserContext);
  const { theme, toggleTheme } = useContext(ThemeContext);

  const handleUpdateUser = () => {
    const newName = prompt("Enter new name:");
    const newAge = prompt("Enter new age:");
    if (newName && newAge) {
      updateUser({ name: newName, age: parseInt(newAge, 10) });
    }
  };

  return (
    <div style={{ background: theme === "light" ? "#fff" : "#333", color: theme === "light" ? "#000" : "#fff" }}>
      <h2>User Profile</h2>
      <p>Name: {user.name}</p>
      <p>Age: {user.age}</p>
      <p>Theme: {theme}</p>
      <button onClick={handleUpdateUser}>Update User</button>
      <button onClick={toggleTheme}>Toggle Theme</button>
    </div>
  );
}

export default UserProfile;

4. アプリケーションの構築

AppProviderをルートコンポーネントで使用し、全体の状態を管理します。

import React from "react";
import ReactDOM from "react-dom";
import { AppProvider } from "./AppProvider";
import UserProfile from "./UserProfile";

function App() {
  return (
    <div>
      <h1>Dynamic Context Example</h1>
      <UserProfile />
    </div>
  );
}

ReactDOM.render(
  <AppProvider>
    <App />
  </AppProvider>,
  document.getElementById("root")
);

5. 演習内容

以下の内容を実践し、理解を深めましょう:

  1. 新しいContextの追加
    「通知設定」を管理するContextを追加し、オン/オフを切り替えられるようにする。
  2. パフォーマンスの最適化
  • 不要な再レンダリングを防ぐために、Contextの値をuseMemoでメモ化する。
  • 値を必要なコンポーネントのみに供給するようProviderを分割する。
  1. TypeScriptでの型定義
  • 各Contextの型を定義して、型安全に値を管理する。

6. 結果と応用

この演習を通じて、以下のスキルを習得できます:

  • Contextを複数管理する方法
  • 状態管理のための適切な設計手法
  • Contextの動的な値更新の実装

これを基に、より複雑なアプリケーションにも応用できるスキルを磨きましょう。

まとめ

本記事では、ReactのContextを使用して値を動的に更新する方法について解説しました。Contextの基本的な仕組みから、useStateuseReducerを活用した値の更新、注意点、そして複数のContextを管理する応用例までを詳しく説明しました。

Contextの効果的な利用は、状態管理を簡潔にし、Props Drillingを回避する強力な手段です。ただし、パフォーマンスや再レンダリングの最適化を考慮し、適切な設計を行うことが重要です。

これらの知識を活かして、Reactアプリケーションの状態管理をより効率的に構築しましょう。

コメント

コメントする

目次
  1. Context APIとは
    1. Context APIの役割
    2. 基本的な仕組み
  2. Contextを利用する際の課題
    1. 1. 再レンダリングの過剰発生
    2. 2. コンテキストの粒度の設計
    3. 3. デバッグの難しさ
    4. 4. Typescriptとの統合
    5. まとめ
  3. Contextの値を動的に更新するための準備
    1. 1. Contextの作成
    2. 2. Providerの作成
    3. 3. アプリケーションにProviderを組み込む
    4. 4. Contextの利用準備が完了
  4. 実装例: 状態を動的に変更するContextの作成
    1. 1. Contextの作成
    2. 2. Providerコンポーネントの作成
    3. 3. 値を使用するコンポーネント
    4. 4. アプリケーション全体でContextを使用
    5. 実行結果
    6. ポイント
  5. Contextの値を更新する仕組み
    1. 1. `useState`を利用した値の更新
    2. 2. `useReducer`を利用した値の更新
    3. 3. 値の消費と更新
    4. 4. 状態管理フックの選択基準
  6. Contextの値を消費するコンポーネントの作成
    1. 1. Contextの値を使用する基本的なコンポーネント
    2. 2. 複数の値を使用するコンポーネント
    3. 3. `Context.Consumer`を利用した例
    4. 4. コンポーネント分割の推奨
    5. まとめ
  7. Context値の動的更新時の注意点
    1. 1. 再レンダリングの最適化
    2. 2. 冗長な再レンダリングの防止
    3. 3. コンポーネントの階層化と依存関係
    4. 4. 非同期処理の管理
    5. 5. デバッグのしやすさ
    6. 6. Typescriptの導入
    7. まとめ
  8. 演習: 複数の値を管理する動的Contextの構築
    1. 1. 要件
    2. 2. Contextの作成
    3. 3. 値を消費するコンポーネントの作成
    4. 4. アプリケーションの構築
    5. 5. 演習内容
    6. 6. 結果と応用
  9. まとめ