You are viewing a preview of this course. Sign in to start learning

React Hooks

React Hooks

Sign In to Start Learning
4
Lessons
147
Questions

Sample Flashcards from This Course

Here's a preview of what you'll learn:

Q1: 💻 You're building a like button for a blog post. You need it to remember whether the user has liked the post. Which Hook should you use? A. useEffect B. useState C. useContext D. useReducer E. useMemo
A: B
Q2: 🔧 Look at this code: ```javascript function Component() { const [count, setCount] = useState(0); if (count > 5) { const [warning, setWarning] = useState(false); } return <div>{count}</div>; } ``` What's wrong with this code? A. useState can only be called once per component B. The Hook is called inside a conditional statement C. count should be initialized with null instead of 0 D. setCount is not being used in the return statement E. warning should be defined outside the if block
A: B
Q3: ⚡ A student writes this timer component: ```javascript function Timer() { const [time, setTime] = useState(0); useEffect(() => { setInterval(() => { setTime(time + 1); }, 1000); }, []); return <div>{time}</div>; } ``` The timer shows 1 and then stops. What's the problem?
A: stale closure
Q4: 📊 You're fetching user data from an API when the component loads. Which dependency array should you use? ```javascript useEffect(() => { fetch('/api/user') .then(res => res.json()) .then(data => setUser(data)); }, ?); ``` A. No dependency array (omit the second argument) B. [] C. [user] D. [setUser] E. [fetch]
A: B
Q5: 🎯 Complete the pattern: In useState, the first element of the returned array is the {{1}}, and the second element is the {{2}}.
A: ["current state value", "setter function"]

Sign in to practice all 147 flashcards with spaced repetition!

Lessons