mcpField
R
react18-string-refs

react18-string-refs

github.com/github開発40.5k741 回インストール1 日前に更新
リポジトリ

react18-string-refs について

React 19で削除される文字列 ref を置き換えるための、React 開発者向け移行スキルです。検出方法と、一般的な ref の書き換えパターンを扱います。

主な機能

  1. 01JSX の割り当てと this.refs の検索
  2. 02React.createRef() への置き換え
  3. 03単一および複数 ref の扱い
  4. 04リスト、コールバック、転送 ref のパターン

活用シーン

  1. 01React コードベース内の文字列 ref の特定
  2. 02React 19 への移行に向けたコンポーネント更新
  3. 03動的 ref や子コンポーネントの ref パターン選択

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/github/awesome-copilot --skill react18-string-refs

インストール後、Claude Code で/react18-string-refsを入力して読み込まれたか確認します。

同梱ファイルreferences/patterns.md

詳細

React 18 文字列 ref の移行

文字列 ref(ref="myInput" + this.refs.myInput)は React 16.3 で非推奨となり、React 18.3.1 では警告が表示され、React 19 で削除されます。

パターン早見表

パターンリファレンス
DOM 要素に対する単一の ref→ patterns.md#single-ref
1 つのコンポーネント内の複数の ref→ patterns.md#multiple-refs
リスト内の ref/動的な ref→ patterns.md#list-refs
コールバック ref(代替アプローチ)→ patterns.md#callback-refs
子コンポーネントに渡す ref→ patterns.md#forwarded-refs

スキャンコマンド

# Find all string ref assignments in JSX
grep -rn 'ref="' src/ --include="*.js" --include="*.jsx" | grep -v "\.test\."

# Find all this.refs accessors
grep -rn "this\.refs\." src/ --include="*.js" --include="*.jsx" | grep -v "\.test\."

両方をまとめて移行する必要があります。各コンポーネントについて、ref="name" と this.refs.name へのアクセスをペアとして検索してください。

移行ルール

すべての文字列 ref を React.createRef() に移行します。

  1. クラスフィールド(またはコンストラクター)として refName = React.createRef(); を追加します
  2. JSX 内の ref="refName" を ref={this.refName} に置き換えます
  3. すべての箇所で this.refs.refName を this.refName.current に置き換えます

各ケースの移行前と移行後の全例については、references/patterns.md を参照してください。

コメント