React Flashcards
Why do you need to import React in order to use JSX?
Because Babel transpiles JSX into React.createElement( )
Why do React component names need to start with a capital letter?
Because this is how React knows it is a React component. Otherwise, it would render it as an HTML tag (i.e., class thing would be rendered as ).
What component lifecycle method(s) are triggered when a component is mounted?
- constructor( ) 2. getDerivedStateFromProps( ) (static) 3. render( ) 4. componentDidMount( )
What component lifecycle method(s) are triggered when a component is updated?
- getDerivedStateFromProps( ) 2. shouldComponentUpdate( ) 3. render( ) 4. getSnapshotBeforeUpdate( ) 5. componentDidUpdate( )
What component lifecycle method(s) are triggered when a component is unmounted?
componentWillUnmount( )
What component lifecycle method(s) are triggered when a component encounters an error during a lifecycle method?
componentDidCatch( )
Why should you fetch data in componentDidMount and not the constructor?
Because the data you need to render might not be available when the component is rendering. By fetching in componentDidMount, the component can render once safely without the fetched data and then again once it has been placed into state.
How might you benefit from “lifting the state up” in a React application.
Placing state high enough allows state values to be shared by sibling components. It also allows you to create purely presentational components.
What is the purpose of useEffect’s return value?
If you return a function from useEffect, React runs it when the component unmounts.
For example, if you add a scroll listener as part of useEffect, you can remove that listener by returning a cleanup function that calls removeEventListener.