What is the purpose of the useEffect hook in React Native?
The useEffect hook lets you run code after the component renders. It helps manage side effects like fetching data, setting up subscriptions.
Click to reveal answer
beginner
How do you run an effect only once when a component mounts using useEffect?
Pass an empty array [] as the second argument to useEffect. This tells React to run the effect only once after the first render.
Click to reveal answer
intermediate
What happens if you omit the dependency array in useEffect?
The effect runs after every render, which can cause performance issues or unwanted repeated actions.
Click to reveal answer
intermediate
How can you clean up side effects in useEffect?
Return a cleanup function inside useEffect. This function runs before the effect runs again or when the component unmounts, useful for removing subscriptions or timers.
Click to reveal answer
intermediate
Explain the role of the dependency array in useEffect.
The dependency array tells React when to re-run the effect. The effect runs only if one of the dependencies changes between renders.
Click to reveal answer
What does passing an empty array [] as the second argument to useEffect do?
ARuns the effect only when the component unmounts
BRuns the effect after every render
CPrevents the effect from running at all
DRuns the effect only once after the first render
✗ Incorrect
An empty dependency array means the effect runs once after the component mounts.
Where should you put cleanup code for subscriptions or timers in useEffect?
AInside the main body of the effect
BOutside the component
CIn a function returned from the effect
DIn the dependency array
✗ Incorrect
Returning a function from useEffect lets React clean up before re-running or unmounting.
What happens if you omit the dependency array in useEffect?
AEffect runs after every render
BEffect runs only once
CEffect never runs
DEffect runs only on unmount
✗ Incorrect
Without dependencies, the effect runs after every render.
Which of these is NOT a typical use case for useEffect?
ADefining a component's state
BSetting up a timer
CSetting up event listeners
DFetching data from an API
✗ Incorrect
State is defined with useState, not useEffect.
If you want an effect to run when a specific variable changes, where do you list that variable?
AInside the effect function
BIn the dependency array
CIn the cleanup function
DIn the component's props
✗ Incorrect
Variables that trigger the effect to re-run go in the dependency array.
Describe how the useEffect hook works and why it is useful in React Native.
Think about when and why you need to do things like fetch data or set timers.
You got /4 concepts.
Explain the importance of the dependency array in useEffect and what happens if it is empty or missing.
Consider how React Native decides when to run your effect.
You got /4 concepts.
Practice
(1/5)
1. What does the useEffect hook do in a React Native component?
easy
A. Defines the component's UI layout
B. Declares a new state variable
C. Runs code after the component renders or updates
D. Handles user input events
Solution
Step 1: Understand the purpose of useEffect
The useEffect hook is designed to run side effects after rendering or updating a component.
Step 2: Compare with other React Native features
Declaring state is done with useState, UI layout is JSX, and event handling uses props or handlers, not useEffect.
Final Answer:
Runs code after the component renders or updates -> Option C
Quick Check:
useEffect runs after render/update = A [OK]
Hint: useEffect runs code after render or update [OK]
Common Mistakes:
Confusing useEffect with useState
Thinking useEffect defines UI
Assuming useEffect handles events directly
2. Which of the following is the correct syntax to run useEffect only once when the component mounts?
easy
A. useEffect(() => { /* code */ });
B. useEffect(() => { /* code */ }, []);
C. useEffect(() => { /* code */ }, true);
D. useEffect(() => { /* code */ }, null);
Solution
Step 1: Identify how to run useEffect once
Passing an empty dependency array [] makes useEffect run only once after the first render.
Step 2: Analyze other options
Omitting the dependency array runs on every render, and passing non-array values like true or null causes a type error since the second argument must be an array.
Final Answer:
useEffect(() => { /* code */ }, []); -> Option B
Quick Check:
Empty array means run once = A [OK]
Hint: Empty array runs useEffect once on mount [OK]
Common Mistakes:
Forgetting the empty array causes repeated runs
Using non-empty arrays thinking it runs once
Omitting the dependency array
3. Consider this React Native code snippet where the setCount calls are in an event handler after initial mount: