Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
useImperativeHandleで理解する クロージャと評価タイミング
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Tasuku Watanabe
January 07, 2026
Programming
82
1
Share
useImperativeHandleで理解する クロージャと評価タイミング
Tasuku Watanabe
January 07, 2026
More Decks by Tasuku Watanabe
See All by Tasuku Watanabe
モックわからないマン卒業記 ~振る舞いを起点に見直した、フロントエンドテストにおけるモックの使いどころ~
tasukuwatanabe
3
470
axiosで作る:ファイルアップロード体験を改善する「Progress Toast」
tasukuwatanabe
0
550
Other Decks in Programming
See All in Programming
セグメントとターゲットを意識するプロポーザルの書き方 〜採択の鍵は、誰に刺すかを見極めるマーケティング戦略にある〜
m3m0r7
PRO
0
550
UIの境界線をデザインする | React Tokyo #15 メイントーク
sasagar
2
360
アクセシビリティ試験の"その後"を仕組み化する
yuuumiravy
0
150
AIエージェントで業務改善してみた
taku271
0
530
今こそ押さえておきたい アマゾンウェブサービス(AWS)の データベースの基礎 おもクラ #6版
satoshi256kbyte
1
250
GNU Makeの使い方 / How to use GNU Make
kaityo256
PRO
16
5.6k
レガシーPHP転生 〜父がドメインエキスパートだったのでDDD+Claude Codeでチート開発します〜
panda_program
0
980
Spec Driven Development | AI Summit Vilnius
danielsogl
PRO
1
100
10年分の技術的負債、完済へ ― Claude Code主導のAI駆動開発でスポーツブルを丸ごとリプレイスした話
takuya_houshima
0
2.6k
Server-Side Kotlin LT大会 vol.18 [Kotlin-lspの最新情報と Neovimのlsp設定例]
yasunori0418
1
150
Radical Imagining - LIFT 2025-2027 Policy Agenda
lift1998
0
370
PDI: Como Alavancar Sua Carreira e Seu Negócio
marcelgsantos
0
120
Featured
See All Featured
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
380
Marketing to machines
jonoalderson
1
5.2k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
340
Scaling GitHub
holman
464
140k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.5k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.2k
How to Talk to Developers About Accessibility
jct
2
180
Building Adaptive Systems
keathley
44
3k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.4k
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
680
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
270
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.7k
Transcript
useImperativeHandleで理解する クロージャと評価タイミング 2026/01/07 MOSH Tech Meetup 株式会社HRBrain 渡邉 佑
株式会社HRBrain 渡邉 佑 新潟県の佐渡島出身 新卒で航海士の道へ進む ウェブ制作やアナリティクスに転向 ボクササイズとサウナが好きです X: @tasuku_web GitHub:
@tasukuwatanabe 1
useImperativeHandleを使ったコンポーネント const VideoPlayer = ({ src, ref }) => {
const videoRef = useRef<HTMLMediaElement>(null) useImperativeHandle(ref, () => ({ get currentTime() { return videoRef.current?.currentTime // 動画の再生位置 } }), []) return <video ref={videoRef} src={src} /> } DOMへの直接アクセスを隠蔽し、必要な機能だけを公開 2
親コンポーネントでの使用 const ParentComponent = () => { const parentRef =
useRef<VideoPlayerRef>(null) const handleClick = () => { parentRef.current?.currentTime // アクセス可能 parentRef.current?.duration // アクセス不可 } return ( <> <VideoPlayer src="video.mp4" ref={parentRef} /> <button onClick={handleClick}>クリック</button> </> ) } 公開するプロパティとメソッドを子コンポーネント側で制限している 3
実装中に遭遇した問題: 「取得される currentTime が常に 0 のまま更新されない」 原因は「値の評価タイミング」にあった 4
NG例①:プロパティの値を変数に代入する useImperativeHandle(parentRef, () => { const currentTime = videoRef.current?.currentTime //
0 const volume = videoRef.current?.volume // 1 return { get currentTime() { return currentTime }, // 0で固定される get volume() { return volume }, // 1で固定される } }, []) 問題: useImperativeHandle実行時に値が評価され、クロージャに固定される → 変数に代入した時点で値がコピーされるため、動画が再生されても更新されない 5
NG例②:DOMへの参照を変数に代入する useImperativeHandle(parentRef, () => { const video = videoRef.current return
{ get currentTime() { return video?.currentTime }, get volume() { return video?.volume }, } }, []) 問題: DOMのマウントが未完了だと、クロージャによって const video = null がキャプチ ャされ、DOMマウント完了後も変数は null のままになる ※ 条件付きレンダリングや遅延マウントなどで発生する可能性 6
解決策:getter内でrefを直接参照 useImperativeHandle(parentRef, () => ({ get currentTime() { return videoRef.current?.currentTime
}, get volume() { return videoRef.current?.volume } }), []) 変数に代入せず、getter内で毎回 videoRef.current を参照 → getter呼び出し時に評価されるため、最新の値を取得でき、マウントタイミングに も依存しない 7
まとめ 変数に代入 → クロージャにキャプチャされ、値や参照が固定される。 評価タイミングを意識して実装しましょう。