React Flashcards
What is Profiler API
A Profiler can be added anywhere in a React tree to measure the cost of rendering that part of the tree. It requires two props: an id (string) and an onRender callback (function) which React calls any time a component within the tree “commits” an update.
render( <App> <Profiler id="Navigation" onRender={callback}> <Navigation {...props} /> </Profiler> <Main {...props} /> </App> );
What methods are called in order during Mounting?
constructor()
static getDerivedStateFromProps()
render()
componentDidMount()
What’s the use of a constructor in React?
Initializing local state by assigning an object to this.state.
Binding event handler methods to an instance.
How to prevent props from being undefined in a class component
call super(props)
before any other statement.
Avoid copying props into state.
constructor(props) { super(props); // Don't do this! this.state = { color: props.color }; }
componentDidMount()
componentDidMount()
is invoked immediately after a component is mounted (inserted into the tree). Initialization that requires DOM nodes should go here. If you need to load data from a remote endpoint, this is a good place to instantiate the network request.
Where should you place subscriptions in class Components
componentDidMount()
is a good place to set up any subscriptions. If you do that, don’t forget to unsubscribe in componentWillUnmount()
.
componentDidUpdate()
componentDidUpdate(prevProps, prevState, snapshot)
is invoked immediately after updating occurs. This method is not called for the initial render. Compare prev and current props before making data requests/calculations, to prevent infinite loops.
Cases where componentDidUpdate()
is not invoked?
componentDidUpdate()
will not be invoked if shouldComponentUpdate()
returns false.
componentWillUnmount()
componentWillUnmount()
is invoked immediately before a component is unmounted and destroyed. Do necessary cleanup in this method, such as invalidating timers, canceling network requests, or cleaning up any subscriptions that were created in componentDidMount()
.
shouldComponentUpdate()
Use shouldComponentUpdate()
to let React know if a component’s output is not affected by the current change in state or props.
This method only exists as a performance optimization. Do not rely on it to “prevent” a rendering, as this can lead to bugs. Consider using the built-in PureComponent
instead of writing shouldComponentUpdate()
by hand. PureComponent performs a shallow comparison of props and state, and reduces the chance that you’ll skip a necessary update.
static getDerivedStateFromProps()
getDerivedStateFromProps
is invoked right before calling the render method, both on the initial mount and on subsequent updates. It should return an object to update the state, or null to update nothing.
getSnapshotBeforeUpdate()
getSnapshotBeforeUpdate()
is invoked right before the most recently rendered output is committed to e.g. the DOM. It enables your component to capture some information from the DOM (e.g. scroll position) before it is potentially changed. Any value returned by this lifecycle method will be passed as a parameter to componentDidUpdate()
.
static getDerivedStateFromError()
This lifecycle is invoked after an error has been thrown by a descendant component. It receives the error that was thrown as a parameter and should return a value to update state.
static getDerivedStateFromError(error) { // Update state so the next render will show the fallback UI. return { hasError: true }; }
componentDidCatch()
This lifecycle is invoked after an error has been thrown by a descendant component. It receives two parameters:
error
- The error that was thrown.info
- An object with a componentStack key containing information about which component threw the error.
What are the legacy Lifecycle Methods
componentWillMount
componentWillReceiveProps
componentWillUpdate
How to work with legacy Lifecycle Methods
adding the following lifecycle aliases: UNSAFE_componentWillMount
, UNSAFE_componentWillReceiveProps
, UNSAFE_componentWillUpdate
.
using, static getDerivedStateFromProps
and getSnapshotBeforeUpdate
.