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
React Hooks を安全に使う
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
kobayang
May 27, 2019
Technology
1.4k
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
React Hooks を安全に使う
kobayang
May 27, 2019
More Decks by kobayang
See All by kobayang
React Suspenseを使って遷移体験を向上させる
kobayang
4
2k
Apollo Client Cache
kobayang
0
1.3k
Puppeteerを導入してみた話
kobayang
2
150k
ゼロから学ぶWeb Authentication API
kobayang
0
1.3k
react-beautiful-dnd を使いたかった話
kobayang
2
2k
Other Decks in Technology
See All in Technology
Reliability in the Age of AI: Engineering for AI Velocity
rrreeeyyy
0
120
AIを「創る」と「使う」の循環 — HRテックが実践するリアルなAI組織実装
taketo957
0
1.9k
2026 TECHFRESH 畢業分享會 - 開發日常大解密!從領域驅動到企業級上線
line_developers_tw
PRO
0
590
就職⽀援サービスにおけるキャリアアドバイザーのシフトスケジューリング
recruitengineers
PRO
1
120
ブロックチェーン / Blockchain
ks91
PRO
0
120
MIERUNE JCT 発表資料「宇宙から伊能忠敬ごっこ」
syuchimu
0
200
2026 TECHFRESH 畢業分享會 - AI-Native 重塑軟體工程與虛擬講師
line_developers_tw
PRO
0
590
タクシーアプリ『GO』の実践的データ活用
mot_techtalk
3
190
2026TECHFRESH畢業分享會 - Lightning Talk - 打造精準高效的 MCP 設計模式與測試實務
line_developers_tw
PRO
0
590
Oracle AI Database@AWS:サービス概要のご紹介
oracle4engineer
PRO
4
2.9k
AmazonRoute 53ではじめてのドメイン取得!HTTPS化までの道のりを整理してみた
usanchuu
3
120
日本 Fintech 未来予測レポート 2027〜2028年(手動編集版)
8maki
0
690
Featured
See All Featured
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.3k
4 Signs Your Business is Dying
shpigford
187
22k
From π to Pie charts
rasagy
0
200
sira's awesome portfolio website redesign presentation
elsirapls
0
280
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
380
YesSQL, Process and Tooling at Scale
rocio
174
15k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.5k
Exploring anti-patterns in Rails
aemeredith
3
400
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
55k
First, design no harm
axbom
PRO
2
1.2k
Fireside Chat
paigeccino
42
3.9k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.2k
Transcript
©2018 Wantedly, Inc. React HooksΛ҆શʹ͏ React.js meetup #7 25.Jan.2019 -
Naoki Kobayashi
©2018 Wantedly, Inc. Naoki Kobayashi GitHub: @kobayang Twitter: @kbys_02 I’m
an Engineer @Wantedly
©2018 Wantedly, Inc. ͜Μͳܦݧ͋Γ·ͤΜ͔ʁ
©2018 Wantedly, Inc. Agenda - Hooks ΛదʹϝϞԽ͢Δ - react-hooks/exhaustive-deps Λઃఆ͢Δ
- stop-runaway-react-effects ͰϧʔϓΛࢹ͢Δ
©2018 Wantedly, Inc. HooksΛదʹϝϞԽ͢Δ
©2018 Wantedly, Inc. e.g. useRect: divͷେ͖͞Λऔಘ͢ΔCustom Hooks export const useRect
= () => { const [rect, setRect] = useState<ClientRect | DOMRect>(); const ref = useRef<HTMLDivElement | null>(null); const resize = () => { const target = ref.current; if (target) { const rect = target.getBoundingClientRect(); setRect(rect); } }; useEffect(() => resize(), [resize]); return { ref, rect }; }
©2018 Wantedly, Inc. e.g. useRect: divͷେ͖͞Λऔಘ͢ΔCustom Hooks export const useRect
= () => { const [rect, setRect] = useState<ClientRect | DOMRect>(); const ref = useRef<HTMLDivElement | null>(null); const resize = () => { const target = ref.current; if (target) { const rect = target.getBoundingClientRect(); setRect(rect); } }; useEffect(() => resize(), [resize]); return { ref, rect }; } resizeؔඳը͝ͱʹੜ͞Εͯ͠·͏
©2018 Wantedly, Inc. e.g. useRect: divͷେ͖͞Λऔಘ͢ΔCustom Hooks export const useRect
= () => { const [rect, setRect] = useState<ClientRect | DOMRect>(); const ref = useRef<HTMLDivElement | null>(null); const resize = useCallback(() => { const target = ref.current; if (target) { const rect = target.getBoundingClientRect(); setRect(rect); } }, []); useEffect(() => resize(), [resize]); return { ref, rect }; } useCallbackͰؔΛϝϞԽ͢Δ
©2018 Wantedly, Inc. e.g. counter const App: React.FC = ()
=> { const [count, setCount] = useState(0); const addCount = useCallback(() => { setCount(count + 1); }, []); return ( <div> <button onClick={() => addCount()}>Add Count</button> <p>count: {count}</p> </div> ); };
©2018 Wantedly, Inc. e.g. counter const App: React.FC = ()
=> { const [count, setCount] = useState(0); const addCount = useCallback(() => { setCount(count + 1); }, []); return ( <div> <button onClick={() => addCount()}>Add Count</button> <p>count: {count}</p> </div> ); }; count ͷࢀর͕ݹ͍
©2018 Wantedly, Inc. e.g. counter const App: React.FC = ()
=> { const [count, setCount] = useState(0); const addCount = useCallback(() => { setCount(count + 1); }, [count]); return ( <div> <button onClick={() => addCount()}>Add Count</button> <p>count: {count}</p> </div> ); }; count ΛୈೋҾʹՃ͢Δ
©2018 Wantedly, Inc. e.g. Hooks ΛదʹϝϞԽ͢Δ - useCallbackΛ༻͠ඳը͝ͱͷؔͷੜΛ͙ - useCallbackͷୈೋҾΛదʹઃఆ͢Δ
- useMemo Ͱಉ༷ Ͳ͏ͬͯ֬ೝ͢Δʁ
©2018 Wantedly, Inc. react-hooks/exhaustive-deps
©2018 Wantedly, Inc. react-hooks/exhaustive-deps ઃఆͯ͠·͔͢ʁ - eslintͷreact-hooksͷrule - ඳը͝ͱʹ͕ؔมߋ͞ΕΔ߹ -
HooksͰࢀর͞ΕΔมΛୈೋҾʹઃఆ͍ͯ͠ͳ͍߹
©2018 Wantedly, Inc. react-hooks/exhaustive-deps ઃఆͯ͠·͔͢ʁ ReactͷެࣜυΩϡϝϯτͰઃఆ͢Δ͜ͱΛ͓קΊ͍ͯ͠Δ IUUQTSFBDUKTPSHEPDTIPPLTSFGFSFODFIUNMVTFF⒎FDU
©2018 Wantedly, Inc. disableʹ͢Δ߹ඞͣཧ༝Λॻ͘ useEffect(() => { // apolloClient ඳը͝ͱʹੜ͞ΕͯΔͨΊઃఆ͠ͳ͍
// eslint-disable-next-line react-hooks/exhaustive-deps }, [paramId]); useEffect(() => { // mount࣌ʹҰ͚࣮ͩߦ͍ͨ͠ͷͰ deps Λۭʹ͍ͯ͠·͢ɻ // eslint-disable-next-line react-hooks/exhaustive-deps }, []);
©2018 Wantedly, Inc. stop-runaway-react-effects
©2018 Wantedly, Inc. exhaustive-deps ͰνΣοΫͰ͖ͳ͍έʔε const handleResize = debounce(resize, 16);
useEffect(() => { handleResize(); window.addEventListener("resize", handleResize); return () => { window.removeEventListener("resize", handleResize); }; }, [handleResize]); →ؒతʹ͕ؔมԽ͢Δ߹
©2018 Wantedly, Inc. stop-runaway-react-effects
©2018 Wantedly, Inc. stop-runaway-react-effects - Ͳͷ effect callback Ͱ ϧʔϓ͕ى͖͍ͯΔ͔
- ͲͷҾ͕มԽ͍ͯ͠Δ͔
©2018 Wantedly, Inc. ·ͱΊ - దʹϝϞԽΛߦ͍ແݶϧʔϓΛආ͚Δ - react-hooks/exhaustive-deps Λઃఆ͢Δ -
stop-runaway-react-effects ͰϧʔϓΛࢹ͢Δ