React Flashcards
(32 cards)
What does the constructor method do ?
This method is called when the component is first created, and is used to initialize state, bind event handlers, and perform other setup tasks.
Which React component phase includes the invoking of the constructor function ?
Mounting
getDerivedStateFromProps(props, state)
This method is called every time a component is re-rendered, and is used to update the component state based on changes to the props.
Which phase includes invoking of getDerivedStateFromProps(props, state)
Mounting and Updating
Which react component phase includes invoking of the render function ?
Mounting and Updating
Which react component phase includes invoking of the componentDidMount method ?
Mounting
purpose of the render method ?
This method is called every time a component needs to be rendered, and returns the JSX to be rendered to the DOM.
purpose of component did mount method ?
This method is called immediately after the component is inserted into the DOM, and is used to perform any setup tasks that require access to the DOM, such as initializing third-party libraries or setting up event listeners.
What happens in the Mounting Phase of a React component ?
Mounting phase: These methods are called when an instance of a component is being created and inserted into the DOM.
- constructor()
- getDerivedStateFromProps(props, state)
- render()
- componentDidMount()
purpose of shouldComponentUpdate(nextProps, nextState):
This method is called before a component is re-rendered, and can be used to prevent unnecessary re-renders by returning false.
purpose of getSnapshotBeforeUpdate(prevProps, prevState):?
This method is called immediately before a component is re-rendered, and can be used to capture some information from the DOM (such as scroll position) before it is updated.
purpose of componentDidUpdate(prevProps, prevState, snapshot) ?
This method is called immediately after a component is re-rendered, and is used to perform any necessary cleanup or additional setup tasks.
What happens in the Updating Phase ? Which methods are called / invoked ?
These methods are called when a component is being re-rendered due to changes in its props or state.
* getDerivedStateFromProps(props, state):
* shouldComponentUpdate(nextProps, nextState):
* render():
* getSnapshotBeforeUpdate(prevProps, prevState):
* componentDidUpdate(prevProps, prevState, snapshot):
purpose of componentWillUnmount() ?
This method is called just before a component is removed from the DOM, and is used to perform any necessary cleanup tasks, such as removing event listeners or canceling network requests.
What happens in the unmounting phase ?
Unmounting phase: This method is called when a component is being removed from the DOM.
—- componentWillUnmount()
purpose of useEffect Hooks
to handle side effects in functional components
What are the two arguements that the useEffect hook takes in ?
a function that represents the side effect to be performed, and an array of dependencies that determine when the effect should be executed.
When will the useEffect Hook get executed ?
after the component is rendered and whenever any of the dependencies in the second argument change.
What would happen if you omit the second argument to the useEffect Hook ?
the effect will be executed after every render.
Which lifecycle methods will the useEffect hook correspond to ?
the componentDidMount, componentDidUpdate, and componentWillUnmount lifecycle methods in class components.
How does the useEffect hook undergo the componentDidMount lifecycle method ?
When the component is first mounted, the effect function is executed after the first render.
How does the useEffect hook undergo the componentDidUpdate lifecycle method ?
When the component updates, the effect is executed again if any of the dependencies have changed.
How does the useEffect hook undergo the componentWillUnmount lifecycle method ?
when the component is unmounted, the effect’s cleanup function is executed.
How does the useEffect support a cleanup function ?
useEffect also supports a cleanup function that allows developers to clean up any resources that were created by the effect. This function is returned from the effect function and is executed when the component unmounts or when the effect is re-executed due to a change in the dependencies.