B. useEffect callback should not be async function
C. setItems should be called outside useEffect
D. useState initial value must be null
Solution
Step 1: Identify async useEffect issue
React's useEffect callback cannot be async directly; it should call an async function inside.
Step 2: Confirm other parts
getDocs usage and setItems inside useEffect are correct; initial state as empty array is valid.
Final Answer:
useEffect callback should not be async function -> Option B
Quick Check:
Async function inside useEffect, not async callback [OK]
Hint: Never make useEffect callback async directly [OK]
Common Mistakes:
Making useEffect callback async directly
Thinking getDocs needs extra arguments
Believing setState must be outside useEffect
5. You want to build a React app that shows a live list of chat messages from Firebase Firestore. Which approach correctly updates the UI in real-time?
hard
A. Use Firestore's onSnapshot listener inside useEffect to update state on data changes
B. Call getDocs repeatedly every second inside useEffect to fetch new messages
C. Fetch messages once on component mount and never update state again
D. Use Firebase Realtime Database instead of Firestore for real-time updates
Solution
Step 1: Understand real-time update methods
Firestore provides onSnapshot to listen to live data changes and update UI immediately.
Step 2: Compare options
Use Firestore's onSnapshot listener inside useEffect to update state on data changes uses onSnapshot inside useEffect, which is best practice for real-time updates in React.
Final Answer:
Use Firestore's onSnapshot listener inside useEffect to update state on data changes -> Option A
Quick Check:
onSnapshot enables real-time UI updates [OK]
Hint: Use onSnapshot listener for live Firestore updates [OK]
Common Mistakes:
Polling with getDocs wastes resources
Fetching once misses updates
Confusing Firestore with Realtime Database necessity
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.