fix(view): 文字列refとfindDOMNodeをcreateRefに置き換え、unmount時にリスナーを解除する - #77
Merged
Conversation
Indexが componentDidMount で登録する popstate リスナーに解除処理が無く、 scroll / resize も bind した別参照で登録して生メソッドで removeEventListener していたため解除できていなかった。littel-ui のようにマウントし直す使い方では unmount 済みインスタンスのリスナーが残り、popstate のたびに剥がされた this.refs.results (undefined) へ setState して 「Cannot read properties of undefined (reading 'setState')」が出ていた。 - 文字列 ref (box / freeword / results) を createRef に置き換える。 freeword と box は元からホスト要素なので findDOMNode は呼び出しごと外せる - リスナーを束縛済み参照でフィールドに持ち、componentWillUnmount で popstate / scroll / resize をすべて解除する。resizeTimer も clearTimeout する - window.pressKey も自分が設定したものなら unmount 時に片付ける - onPopState が setState に渡していた sort_key はタイポ (正しくは sort_column)。 popstate で戻った時にソート列がリセットされていなかった - onSort / onSelectBook の引数型を React.SyntheticEvent に直す。 onChange 用の型 (ChangeEvent) のまま onClick / onKeyUp に渡されていて、 strictBindCallApply を有効にすると型エラーになる唯一の箇所だった - Index をクライアント描画するテストを追加。unmount 後にリスナーが 反応しないことを検証する (修正前のコードでは5件すべて落ちる) createRef は React 16.3 以降の API なので React 18 据え置きのまま入れられる。 これで littel-ui / unitrad-kintone-plugin / unitrad-ui-nagano が React 19 に 上げられない原因 (findDOMNode と文字列 ref) が上流から消える。 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ERVipW4efqpQTjkKaqVoXr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
何が問題か
2つの問題が同じファイルに同居していた。
1. unmount してもリスナーが残る。
IndexがcomponentDidMountで登録する popstate リスナーに解除処理が無い。scroll / resize もbind(this)した別参照で登録して生メソッドでremoveEventListenerしているため、解除できていなかった。littel-ui のように Unitrad を開閉のたびにマウントし直す使い方では、unmount 済みインスタンスの popstate リスナーが残留し、戻る・進むのたびに剥がされた
this.refs.results(unmount 時に delete されて undefined)へ setState してCannot read properties of undefined (reading 'setState')が出ていた。littel-ui 側で観測されていたコンソール例外の原因はこれ。2. React 19 に上げられない原因が残っている。 文字列 ref と
findDOMNodeは React 19 で削除済み。ここが直らないと、このコードを内包する littel-ui / unitrad-kintone-plugin / unitrad-ui-nagano も上げられない。変更点
box/freeword/results)をcreateRefに置き換えた。boxとfreewordは元からホスト要素を指しているのでfindDOMNodeは呼び出しごと外した。createRefは React 16.3 以降の API なので React 18 据え置きのまま入れられるcomponentWillUnmountで popstate / scroll / resize をすべて解除するようにした。resizeTimerも clearTimeout する。window.pressKeyも自分が設定したものなら片付けるonPopStateが setState に渡していたsort_keyはタイポ(Results の state はsort_column)。popstate で戻った時にソート列がリセットされていなかったのを直したonSort/onSelectBookの引数型をReact.SyntheticEventに直した。onChange 用の型(ChangeEvent<HTMLInputElement>)のまま a 要素の onClick / onKeyUp に渡されていて、strictBindCallApplyを有効にすると型エラーになる唯一の箇所だった(littel-ui 側で tsconfig の strictness を上げる作業のブロッカー)テスト
Indexをクライアント描画するテストを追加した。既存の test_view.mts は renderToString だけで、componentDidMount が走らないためこの種の回帰を検出できなかった。修正前のコードに対してはこの5件がすべて落ちることを確認済み(リスナー未解除と resultsRef 不在で fail する)。
確認したこと
npm run typecheck/npm test(190件)が通るnpm run releaseが通る(194,011 bytes)🤖 Generated with Claude Code
https://claude.ai/code/session_01ERVipW4efqpQTjkKaqVoXr