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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Tasuku Watanabe
January 07, 2026
Programming
1
74
useImperativeHandleで理解する クロージャと評価タイミング
Tasuku Watanabe
January 07, 2026
Tweet
Share
More Decks by Tasuku Watanabe
See All by Tasuku Watanabe
axiosで作る:ファイルアップロード体験を改善する「Progress Toast」
tasukuwatanabe
0
460
Other Decks in Programming
See All in Programming
KIKI_MBSD Cybersecurity Challenges 2025
ikema
0
1.3k
AI時代の認知負荷との向き合い方
optfit
0
160
コントリビューターによるDenoのすゝめ / Deno Recommendations by a Contributor
petamoriken
0
200
Fluid Templating in TYPO3 14
s2b
0
130
【卒業研究】会話ログ分析によるユーザーごとの関心に応じた話題提案手法
momok47
0
190
CSC307 Lecture 01
javiergs
PRO
0
690
AIフル活用時代だからこそ学んでおきたい働き方の心得
shinoyu
0
130
Amazon Bedrockを活用したRAGの品質管理パイプライン構築
tosuri13
4
280
高速開発のためのコード整理術
sutetotanuki
1
390
Smart Handoff/Pickup ガイド - Claude Code セッション管理
yukiigarashi
0
130
副作用をどこに置くか問題:オブジェクト指向で整理する設計判断ツリー
koxya
1
600
0→1 フロントエンド開発 Tips🚀 #レバテックMeetup
bengo4com
0
560
Featured
See All Featured
AI Search: Where Are We & What Can We Do About It?
aleyda
0
6.9k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
180
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
160
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
270
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.6k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.8k
Are puppies a ranking factor?
jonoalderson
1
2.7k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
300
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.4k
Docker and Python
trallard
47
3.7k
Producing Creativity
orderedlist
PRO
348
40k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
180
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
まとめ 変数に代入 → クロージャにキャプチャされ、値や参照が固定される。 評価タイミングを意識して実装しましょう。