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
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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
[モダンアプリ勉強会]今更聞けないGit/GitHub入門
tsukuboshi
0
360
Bucharest Tech Week 2026 - Reinventing testing practices in the AI era
edeandrea
PRO
1
140
2026.06.13_AI時代に事業会社が「SIer出身エンジニア」を求める理由 / Why Businesses Seek Engineers with a System Integrator Background in the AI Era
jumtech
0
1k
AIソロプレナー時代に2ヶ月で20人増員した事業創造会社の開発組織の話
miyatakoji
0
570
AGENTS.mdとSkillsで始めるAIエージェント活用
sonoda_mj
2
190
Chainlitで作るお手軽チャットUI
ynt0485
0
170
Agentic Web
dynamis
1
200
日本 Fintech 未来予測レポート 2027〜2028年(手動編集版)
8maki
0
1.5k
小さく始める AI 活用推進 ― 日経電子版 Web チームの事例/nikkei-tech-talk47
nikkei_engineer_recruiting
0
200
Djangoユーザが知っ得なPostgreSQL機能 - 設計の選択肢を増やす / Djang-use-PostgreSQL
soudai
PRO
1
230
爆速でマルチプロダクトを立ち上げる時 事業・CTO目線で大事にしたい事
miyatakoji
0
100
Claude Code×Terraform IaC テンプレート駆動開発
itouhi
1
490
Featured
See All Featured
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
Designing for Timeless Needs
cassininazir
1
250
The browser strikes back
jonoalderson
0
1.2k
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.5k
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
160
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
Become a Pro
speakerdeck
PRO
31
6k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
410
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.1k
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
Fireside Chat
paigeccino
42
3.9k
Music & Morning Musume
bryan
47
7.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 ͰϧʔϓΛࢹ͢Δ