Top Banner
Something about Lifecycle
48

React Lifecycle and Reconciliation

Jul 27, 2015

Download

Software

Zhihao Li
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: React Lifecycle and Reconciliation

Something about Lifecycle

Page 2: React Lifecycle and Reconciliation

Something about Lifecycle

componentWillMount

Page 3: React Lifecycle and Reconciliation

Something about Lifecycle

componentWillMount

componentDidMount

Page 4: React Lifecycle and Reconciliation

Something about Lifecycle

componentWillMount

componentDidMount

Mounting

Page 5: React Lifecycle and Reconciliation

Something about Lifecycle

componentWillMount

componentDidMount

Mounting

render

Page 6: React Lifecycle and Reconciliation

Something about Lifecycle

Page 7: React Lifecycle and Reconciliation

Something about Lifecycle

componentWillReceiveProps

Page 8: React Lifecycle and Reconciliation

Something about Lifecycle

componentWillReceiveProps

shouldComponentUpdate

Page 9: React Lifecycle and Reconciliation

Something about Lifecycle

componentWillReceiveProps

shouldComponentUpdate

componentWillUpdate

Page 10: React Lifecycle and Reconciliation

Something about Lifecycle

componentWillReceiveProps

shouldComponentUpdate

Pre-updating

componentWillUpdate

Page 11: React Lifecycle and Reconciliation

Something about Lifecycle

componentWillReceiveProps

shouldComponentUpdate

Pre-updating

render

componentWillUpdate

Page 12: React Lifecycle and Reconciliation

Something about Lifecycle

Page 13: React Lifecycle and Reconciliation

Something about Lifecyclerender

Page 14: React Lifecycle and Reconciliation

Something about Lifecycle

componentDidUpdate

render

Page 15: React Lifecycle and Reconciliation

Something about Lifecycle

componentDidUpdatePost-updating

render

Page 16: React Lifecycle and Reconciliation

Something about Lifecycle

componentDidUpdate

componentWillUnmount

Post-updating

render

Page 17: React Lifecycle and Reconciliation

Something about Lifecycle

componentDidUpdate

componentWillUnmount

Post-updating

render

Unmounting

Page 18: React Lifecycle and Reconciliation
Page 19: React Lifecycle and Reconciliation

The usage of React is intuitive

Page 20: React Lifecycle and Reconciliation

The usage of React is intuitiveWe re-render whenever state changes

Page 21: React Lifecycle and Reconciliation

The usage of React is intuitiveWe re-render whenever state changes

Re-rendering seems expensive…

Page 22: React Lifecycle and Reconciliation

The usage of React is intuitiveWe re-render whenever state changes

Re-rendering seems expensive…But the truth is

Page 23: React Lifecycle and Reconciliation

The usage of React is intuitive

React is FAST!!!

We re-render whenever state changesRe-rendering seems expensive…

But the truth is

Page 24: React Lifecycle and Reconciliation

The usage of React is intuitive

React is FAST!!!

We re-render whenever state changesRe-rendering seems expensive…

But the truth is

WTF???

Page 25: React Lifecycle and Reconciliation

BatchingSub-tree Rendering

Page 26: React Lifecycle and Reconciliation

Batching• Whenever you call setState on a component,

React will mark it as dirty. At the end of the event loop, React looks at all the dirty components and re-renders them.

• This batching means that during an event loop, there is exactly one time when the DOM is being updated. This property is key to building a performant app and yet is extremely difficult to obtain using commonly written JavaScript. In a React application, you get it by default.

Page 27: React Lifecycle and Reconciliation

Batching• Whenever you call setState on a component,

React will mark it as dirty. At the end of the event loop, React looks at all the dirty components and re-renders them.

• This batching means that during an event loop, there is exactly one time when the DOM is being updated. This property is key to building a performant app and yet is extremely difficult to obtain using commonly written JavaScript. In a React application, you get it by default.

Page 28: React Lifecycle and Reconciliation

Sub-tree Rendering• Whenever you call setState on a component,

React will mark it as dirty. At the end of the event loop, React looks at all the dirty components and re-renders them.

• This batching means that during an event loop, there is exactly one time when the DOM is being updated. This property is key to building a performant app and yet is extremely difficult to obtain using commonly written JavaScript. In a React application, you get it by default.

Page 29: React Lifecycle and Reconciliation

Sub-tree Rendering• Whenever you call setState on a component,

React will mark it as dirty. At the end of the event loop, React looks at all the dirty components and re-renders them.

• This batching means that during an event loop, there is exactly one time when the DOM is being updated. This property is key to building a performant app and yet is extremely difficult to obtain using commonly written JavaScript. In a React application, you get it by default.

With shouldComponentUpdate, you have control over whether a component should be re-rendered.

Page 30: React Lifecycle and Reconciliation

Sub-tree Rendering• Whenever you call setState on a component,

React will mark it as dirty. At the end of the event loop, React looks at all the dirty components and re-renders them.

• This batching means that during an event loop, there is exactly one time when the DOM is being updated. This property is key to building a performant app and yet is extremely difficult to obtain using commonly written JavaScript. In a React application, you get it by default.

shouldComponentUpdateThis function is going to be called all the time, so you want to make sure that it takes less time to compute the heuristic than the time it would have taken to render the component, even if re-rendering was not strictly needed.

Page 31: React Lifecycle and Reconciliation

React Reconciliation

Page 32: React Lifecycle and Reconciliation

React Reconciliation

a.k.a. Diff Algorithm

Page 33: React Lifecycle and Reconciliation

Why Another Tree Diff Algorithm?

Page 34: React Lifecycle and Reconciliation

Why Another Tree Diff Algorithm?

O(n 3)

Page 35: React Lifecycle and Reconciliation

Why Another Tree Diff Algorithm?

O(n 3)

1000 nodes would require in the order of

ONE BILLIONcomparisons.

Page 36: React Lifecycle and Reconciliation

2 Basic Assumptions

Page 37: React Lifecycle and Reconciliation

2 Basic Assumptions

• Two components of the same class will generate similar trees and two components of different classes will generate different trees.

Page 38: React Lifecycle and Reconciliation

2 Basic Assumptions

• Two components of the same class will generate similar trees and two components of different classes will generate different trees.

• It is possible to provide a unique key for elements that is stable across different renders.

Page 39: React Lifecycle and Reconciliation

Pair-wise Diff Different Node Types

renderA: <div />renderB: <span />=> [removeNode <div />], [insertNode <span />]

Code

renderA: <Header />renderB: <Content />=> [removeNode <Header />], [insertNode <Content />]

Code

Page 40: React Lifecycle and Reconciliation

Pair-wise Diff DOM Nodes

renderA: <div id="before" />renderB: <div id="after" />=> [replaceAttribute id "after"]

Code

renderA: <div style={{color: 'red'}} />renderB: <div style={{fontWeight: 'bold'}} />=> [removeStyle color], [addStyle font-weight 'bold']

Code

Page 41: React Lifecycle and Reconciliation

List-wise Diff Problematic Case

renderA: <div><span>first</span></div>renderB: <div><span>first</span><span>second</span></div>=> [insertNode <span>second</span>]

Code

renderA: <div><span>first</span></div>renderB: <div><span>second</span><span>first</span></div>=> [replaceAttribute textContent 'second'], [insertNode <span>first</span>]

Code

Page 42: React Lifecycle and Reconciliation

List-wise Diff Problematic Case

renderA: <div><span>first</span></div>renderB: <div><span>first</span><span>second</span></div>=> [insertNode <span>second</span>]

Code

renderA: <div><span>first</span></div>renderB: <div><span>second</span><span>first</span></div>=> [replaceAttribute textContent 'second'], [insertNode <span>first</span>]

Code

Levenshtein DistanceO(n

2)

Page 43: React Lifecycle and Reconciliation

List-wise Diff Keys

renderA: <div><span key="first">first</span></div>renderB: <div><span key="second">second</span><span key="first">first</span></div>=> [insertNode <span>second</span>]

Code

If you specify a key, React is now able to find insertion, deletion, substitution and moves in O(n) using a hash table.

Page 44: React Lifecycle and Reconciliation

Trade-offs

Page 45: React Lifecycle and Reconciliation

Trade-offs• The algorithm will not try to match sub-trees of different

components classes. If you see yourself alternating between two components classes with very similar output, you may want to make it the same class. In practice, we haven't found this to be an issue.

Page 46: React Lifecycle and Reconciliation

Trade-offs• The algorithm will not try to match sub-trees of different

components classes. If you see yourself alternating between two components classes with very similar output, you may want to make it the same class. In practice, we haven't found this to be an issue.

• If you don't provide stable keys (by using Math.random() for example), all the sub-trees are going to be re-rendered every single time. By giving the users the choice to choose the key, they have the ability to shoot themselves in the foot.

Page 47: React Lifecycle and Reconciliation

Where to go from here? & Acknowledgements

• Official Documentation: https://facebook.github.io/react/

• React’s diff algorithm: http://calendar.perfplanet.com/2013/diff/

• React Demystified: http://blog.reverberate.org/2014/02/react-demystified.html

Page 48: React Lifecycle and Reconciliation

Thanks