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
Tasuku Watanabe
January 07, 2026
Programming
100
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
540
axiosで作る:ファイルアップロード体験を改善する「Progress Toast」
tasukuwatanabe
0
600
Other Decks in Programming
See All in Programming
Build-to-own AI: Agentic Development for Humans
inesmontani
PRO
0
110
Apache Hive: そしてCloud Native Lakehouseへ
okumin
1
180
20260722_microCMSで考える、AI時代のコンテンツ運用設計
yosh1
0
150
<title><a id="</title>君はこのHTMLをパースできるか"></a></title> #雑LT_study
pizzacat83
0
110
torikago - Ruby::Boxで照らすモジュラモノリスの実行境界
se4weed
1
240
壊れたパーサから始める関数型設計と構成的なパーサ #fp_matsuri
raiga0310
2
410
PHPだって関数型したい 〜できること、できないこと〜 / fp-in-php
jsoizo
1
250
yield再入門 #phpcon
o0h
PRO
0
820
継続モナドとリアクティブプログラミング
yukikurage
3
650
Embedded SREと共に達成した会員管理システムのAWS移行 - SRE NEXT 2026 ランチスポンサーセッション
niftycorp
PRO
1
3.2k
テーブルをDELETEした
yuzneri
0
120
Built Our Own Background Agent at LayerX #aidevex_findy
layerx
PRO
9
3.7k
Featured
See All Featured
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.3k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
560
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
910
Designing Experiences People Love
moore
143
24k
Marketing to machines
jonoalderson
1
5.6k
How to Ace a Technical Interview
jacobian
281
24k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
56k
Rails Girls Zürich Keynote
gr2m
96
14k
VelocityConf: Rendering Performance Case Studies
addyosmani
333
25k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.8k
Into the Great Unknown - MozCon
thekraken
41
2.6k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
270
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
まとめ 変数に代入 → クロージャにキャプチャされ、値や参照が固定される。 評価タイミングを意識して実装しましょう。