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
wejs24.pdf
Search
Kodai Nakamura
September 25, 2018
1.2k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
wejs24.pdf
Kodai Nakamura
September 25, 2018
More Decks by Kodai Nakamura
See All by Kodai Nakamura
React Fire
kdnk
2
1.5k
Local State Management with Apollo
kdnk
2
1.6k
React Suspense on Apollo
kdnk
4
1.6k
Featured
See All Featured
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
990
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.5k
Designing Powerful Visuals for Engaging Learning
tmiket
1
500
From π to Pie charts
rasagy
0
300
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
The Curse of the Amulet
leimatthew05
2
14k
The SEO identity crisis: Don't let AI make you average
varn
0
530
Everyday Curiosity
cassininazir
0
290
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
210
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
210
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
470
YesSQL, Process and Tooling at Scale
rocio
174
15k
Transcript
©2018 Wantedly, Inc. setState Deep Dive Wejs@24 21.Sept.2018 - Kodai
Nakamura
©2018 Wantedly, Inc. setState Deep Dive Wejs@24 21.Sept.2018 - Kodai
Nakamura
©2018 Wantedly, Inc. Kodai Nakamura Github: @kdnk Twitter: @kdnk__ Software
Engineer at Wantedly, inc. JavaScript, TypeScript, React, Rails, Ruby
©2018 Wantedly, Inc. setState
©2018 Wantedly, Inc. increaseByTwo() { this.setState({ value: this.state.value + 1
}); this.setState({ value: this.state.value + 1 }); } render() { return ( <button onClick={this.increaseByTwo.bind(this)}> {this.state.value} </button> ); }
©2018 Wantedly, Inc.
©2018 Wantedly, Inc.
©2018 Wantedly, Inc. ‣ ίϯϙʔωϯτ͝ͱʹ࣋ͯΔঢ়ଶͷ͜ͱ ‣ state ͕มΘΔͱ࠶ඳը͞ΕΔ ‣ setState
͕͋ͨΒ͍͠ state Λฦ͢ state
©2018 Wantedly, Inc. ‣ ΦϒδΣΫτ͕มΘΔͱre-render ‣ stateʹରͯ͠ഁյతͳมߋ͠ͳ͍ ‣ setStateΛ͏͜ͱͰผͷΦϒδΣΫτΛฦ͢ ‣
stateͷߋ৽batch setStateͷ͍ํ this.setState({comment: 'Hello'}); this.state.comment = 'Hello';
©2018 Wantedly, Inc. ҾʹΦϒδΣΫτΛ͢ increaseByTwo() { this.setState({ value: this.state.value +
1 }); this.setState({ value: this.state.value + 1 }); } render() { return ( <button onClick={this.increaseByTwo.bind(this)}> {this.state.value} </button> ); } ‣ ΦϒδΣΫτ͕มΘΔͱre-render ‣ stateʹରͯ͠ഁյతͳมߋ͠ͳ͍ ‣ setStateΛ͏͜ͱͰผͷΦϒδΣΫτΛฦ͢ ‣ stateͷߋ৽batch
©2018 Wantedly, Inc. ͕͜͜batch increaseByTwo() { this.setState({ value: this.state.value +
1 }); this.setState({ value: this.state.value + 1 }); } render() { return ( <button onClick={this.increaseByTwo.bind(this)}> {this.state.value} </button> ); } ҾʹΦϒδΣΫτΛ͢ ‣ ΦϒδΣΫτ͕มΘΔͱre-render ‣ stateʹରͯ͠ഁյతͳมߋ͠ͳ͍ ‣ setStateΛ͏͜ͱͰผͷΦϒδΣΫτΛฦ͢ ‣ stateͷߋ৽batch
©2018 Wantedly, Inc. { ...this.state.value, value: this.state.value + 1, value:
this.state.value + 1, } ͕͜͜batch increaseByTwo() { this.setState({ value: this.state.value + 1 }); this.setState({ value: this.state.value + 1 }); } render() { return ( <button onClick={this.increaseByTwo.bind(this)}> {this.state.value} </button> ); } தͰ͜Μͳॲཧ͕ߦΘΕ͍ͯΔͣ ҾʹΦϒδΣΫτΛ͢ ‣ ΦϒδΣΫτ͕มΘΔͱre-render ‣ stateʹରͯ͠ഁյతͳมߋ͠ͳ͍ ‣ setStateΛ͏͜ͱͰผͷΦϒδΣΫτΛฦ͢ ‣ stateͷߋ৽batch
©2018 Wantedly, Inc. ‣ setStateͷҾʹΦϒδΣΫτ͚ͩͰͳ͘ɺؔΛ͢͜ͱͰ͖Δ ‣ ؔͷ࣮ߦඇಉظ ҾʹؔΛ͢ increaseByTwoCb() {
this.setState(prevState => ({ value: prevState.value + 1 })); this.setState(prevState => ({ value: prevState.value + 1 })); } render() { return ( <button onClick={this.increaseByTwo.bind(this)}> {this.state.value} </button> ); }
©2018 Wantedly, Inc. ‣ setStateͷҾʹΦϒδΣΫτ͚ͩͰͳ͘ɺؔΛ͢͜ͱͰ͖Δ ‣ ؔͷ࣮ߦඇಉظ increaseByTwoCb() { this.setState(prevState
=> ({ value: prevState.value + 1 })); this.setState(prevState => ({ value: prevState.value + 1 })); } render() { return ( <button onClick={this.increaseByTwo.bind(this)}> {this.state.value} </button> ); } - ͕ؔΩϡʔʹՃ͞ΕΔ - prevState࠷৽Ͱ͋Δ͜ͱ͕ อূ͞ΕΔ ҾʹؔΛ͢
©2018 Wantedly, Inc. ‣ setState ͕ batching ͞ΕΔͷΠϕϯτϋϯυϥͷத͚ͩ ‣ ΠϕϯτϋϯυϥҎ֎ͩͱ
setState ͙͢ʹ࣮ߦ͞Εͯɺ͕ state ʹө͞ΕΔ ‣ ReactDOM.unstable_batchedUpdates Λ͏ͱΉΓΓ batching Ͱ͖Δ ‣ React 17(?) ͔Β ͯ͢ͷ setState ͕σϑΥϧτͰ batching ಉظతͳsetState
©2018 Wantedly, Inc. ‣ setState batch & ඇಉظ ‣
ͨͩ͠ɺΠϕϯτϋϯυϥҎ֎ͩͱɺಉظత ‣ React v17͔Βɺͯ͢ͷ setState ͕ඇಉظʹͳΔ ·ͱΊ