Skip to content
⚠️ This article was written in 2020. Some content may be outdated.

React useReducer による状態管理の詳解

日常開発において、React useReducer による状態管理の使用頻度はますます高まっています。本記事では使い方、原理、最適化戦略を体系的に解説します。

クイックスタート

実際のプロジェクトでの使い方はより複雑になります:

javascript
import { useRef, useEffect, useState } from "react";

function useIntersectionObserver(options = {}) {
  const [isVisible, setIsVisible] = useState(false);
  const ref = useRef(null);

  useEffect(() => {
    const observer = new IntersectionObserver(
      ([entry]) => {
        setIsVisible(entry.isIntersecting);
      },
      { threshold: 0.1, ...options },
    );
    const el = ref.current;
    if (el) observer.observe(el);
    return () => {
      if (el) observer.unobserve(el);
    };
  }, []);

  return [ref, isVisible];
}

この方法でコードのテスト容易性と拡張性が向上します。

内部原理

完全な例を示します:

javascript
import { useRef, useEffect, useState } from "react";

function useIntersectionObserver(options = {}) {
  const [isVisible, setIsVisible] = useState(false);
  const ref = useRef(null);

  useEffect(() => {
    const observer = new IntersectionObserver(
      ([entry]) => {
        setIsVisible(entry.isIntersecting);
      },
      { threshold: 0.1, ...options },
    );
    const el = ref.current;
    if (el) observer.observe(el);
    return () => {
      if (el) observer.unobserve(el);
    };
  }, []);

  return [ref, isVisible];
}

境界条件の処理に注意してください。これは本番環境で非常に重要です。

実務での応用

コアロジックの理解が鍵です:

javascript
import { useRef, useEffect, useState } from "react";

function useIntersectionObserver(options = {}) {
  const [isVisible, setIsVisible] = useState(false);
  const ref = useRef(null);

  useEffect(() => {
    const observer = new IntersectionObserver(
      ([entry]) => {
        setIsVisible(entry.isIntersecting);
      },
      { threshold: 0.1, ...options },
    );
    const el = ref.current;
    if (el) observer.observe(el);
    return () => {
      if (el) observer.unobserve(el);
    };
  }, []);

  return [ref, isVisible];
}

パフォーマンス最適化は具体的なシナリオと組み合わせる必要があります。すべての場合で過度な最適化は不要です。

まとめ

  • コード例は参考のみです。ビジネスシナリオに応じて調整してください

MIT Licensed