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

React PortalでModalコンポーネントを実装する

最近チームで React Portal を使った Modal コンポーネントの実装を進め、いくつかの知見が溜まった。似たような取り組みをする人の参考になればとまとめておく。

コアコンセプト ​

実際のプロジェクトでの使い方はもう少し複雑になる:

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]
}

性能最適化は具体的な場面に合わせる必要があり、すべての場合で過剰な最適化が必要なわけではない。

调优策略 ​

次の方法で改善できる:

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]
}

このアプローチは本番環境で半年以上安定して稼働しており、実際に検証済みだ。

まとめ ​

  • コード例はあくまで参考であり、業務の場面に合わせて調整する必要がある
  • React Portal での Modal 実装は銀の弾丸ではなく、プロジェクトの規模や技術スタックに応じて選ぶべき
  • API を暗記するより、その背後にある原理を理解するほうが重要
  • 本番環境で使う前には必ず互換性の検証を行う
  • チーム開発では、技術そのものよりも取り決めとドキュメントのほうが重要

MIT Licensed