State and Lifecycle
Encapulsate changes in the compoonent
Local State is dict like props, but private and fully controlled by component and not setup by React
Can pass state down to child through props
The order of updates is always respected.
Lifecycle Hooks
Mounting in React with
componentDidMount()
runs when first rendering component to DOMunmounting with
componentWillUnmount()
runs before destroyedupdate with
componentDidUpdate(prevProps, prevState, snapshot)
not initial render
Create a Ticking Clock
Order of execution
Constructor
Render
to DOMcomponentDidMount
Tick
called every second callingsetState()
render() recalled b/c
setState()
if compoonent removed,
componentWillUnmount
SetState()
Use setState() to set state, except in constructor
setState() is async and multiple calls could be buffer
Use overloaded setState() to rely on current state/props
setState() merges given dict with current state overriding keys
Usage
Data flows down, state can only affect components below
State vs stateless components are implementation detail and should be interchangeable
Last updated