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
110
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
useImperativeHandleで理解する クロージャと評価タイミング
Tasuku Watanabe
January 07, 2026
More Decks by Tasuku Watanabe
See All by Tasuku Watanabe
モックわからないマン卒業記 ~振る舞いを起点に見直した、フロントエンドテストにおけるモックの使いどころ~
tasukuwatanabe
3
570
axiosで作る:ファイルアップロード体験を改善する「Progress Toast」
tasukuwatanabe
0
620
Other Decks in Programming
See All in Programming
ハーネス設計入門 〜プロンプト、コンテキストの次〜
kinopeee
54
35k
Press start. Python's next generation.
willingc
PRO
3
310
AIの中の人になってみる
htkym
0
160
AIエージェント時代のコードレビューを設計する
nogu66
6
2.5k
LL言語やWebフレームワークのPostgreSQL対応 〜DBの機能がユーザーに届くまで〜
kentaroutakeda
0
140
「AI時代、配布するPythonコードをどう守るか: 難読化の実験と判断軸」 #PyconJP2026
pkshadeck
PRO
2
160
Deep dive into the select statement (GopherCon UK)
jespino
0
170
AWS Step Functions 大規模並列の壁を越える / jaws-sonic-2026-niigata-step-functions
kasacchiful
PRO
0
370
Discordを用いたラボオートメーション関連情報収集の自動化
noguhiro2002
0
500
PHPプロジェクトの結合バランスを可視化する #php_night
kajitack
0
220
Hello, Hiroshima Geospatial Data! — Exploring DoboX with Python
ra0kley
0
170
PyConJP2026_wat_Python × Signal Processing: How to Draw Pictures with Sound Using Spectrogram Art
wat
0
670
Featured
See All Featured
Writing Fast Ruby
sferik
630
63k
Google's AI Overviews - The New Search
badams
0
1.6k
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
510
KATA
mclloyd
PRO
35
15k
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
370
Marketing to machines
jonoalderson
1
5.7k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
430
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Designing Powerful Visuals for Engaging Learning
tmiket
1
530
The untapped power of vector embeddings
frankvandijk
2
1.9k
Design in an AI World
tapps
1
300
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
まとめ 変数に代入 → クロージャにキャプチャされ、値や参照が固定される。 評価タイミングを意識して実装しましょう。