useEffect Flashcards
True or False: The useEffect hook runs after every render of the component.
True
What is the primary purpose of the useEffect hook in React?
To perform side effects in functional components.
True or False: useEffect runs after every render.
True.
Fill in the blank: useEffect takes two arguments: a function and __________.
an array of dependencies.
What is the default behavior of useEffect if no dependency array is provided?
It runs after every render.
What is the purpose of the dependency array in useEffect?
To control when the effect runs based on changes to specified values.
Multiple Choice: Which of the following is a valid useEffect syntax?
A) const effect = () => useEffect( /* effect */ )
B) useEffect(() => { /* effect */ }, [dependencies]
C) const { useEffect } = () => { /* effect */ }
D) function useEffect () { /* effect */ }
B) useEffect(() => { /* effect */ }, [dependencies]);
What happens if an empty array is passed as the second argument to useEffect?
The effect runs only once after the initial render.
True or False: useEffect can be used to fetch data.
True.
What is the cleanup function in useEffect used for?
To clean up subscriptions or side effects when the component unmounts or before the effect runs again.
Fill in the blank: useEffect is called ________ when the component mounts.
after
What will happen if the dependency array contains a state variable that changes frequently?
The effect will run every time that state variable changes.
Multiple Choice: Which hook can be used in conjunction with useEffect for state management?
A) useReducer
B) useContext
C) useState
D) useRef
C) useState.
Short Answer: What is the syntax to return a cleanup function from useEffect?
return () => { /* cleanup code */ };
True or False: You can call useEffect conditionally within a component.
False.
What is a common use case for useEffect?
Fetching data from an API.
Which of the following is NOT a valid use case for useEffect?
(A) Fetching data
(B) Updating the DOM directly
(C) Setting up a subscription
(D) Returning a value from a component
D) Returning a value from a component.