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
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Exploring anti-patterns in Rails
aemeredith
3
450
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.5k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
460
Optimising Largest Contentful Paint
csswizardry
37
3.8k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
190
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
2.1k
Fashionably flexible responsive web design (full day workshop)
malarkey
408
67k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
430
Raft: Consensus for Rubyists
vanstee
141
7.6k
Faster Mobile Websites
deanohume
310
32k
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 ͕ඇಉظʹͳΔ ·ͱΊ