0
0
Reactframework~20 mins

Reusing logic with hooks in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Hook Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this custom hook usage?
Consider this React component using a custom hook that counts clicks. What will be displayed after clicking the button 3 times?
React
import React, { useState } from 'react';

function useClickCounter() {
  const [count, setCount] = useState(0);
  function increment() {
    setCount(c => c + 1);
  }
  return { count, increment };
}

export default function Clicker() {
  const { count, increment } = useClickCounter();
  return (
    <>
      <button onClick={increment}>Click me</button>
      <p>Clicked {count} times</p>
    </>
  );
}
AClicked undefined times
BClicked 3 times
CClicked NaN times
DClicked 0 times
Attempts:
2 left
💡 Hint
Think about how the state updates with each click and how the count is displayed.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a custom hook that returns a boolean toggle?
You want a custom hook named useToggle that returns a boolean state and a function to flip it. Which code is correct?
A
function useToggle() {
  const [value, setValue] = useState(false);
  const toggle = () =&gt; setValue(value =&gt; !value);
  return { value, toggle };
}
B
function useToggle() {
  const [value, setValue] = useState(false);
  const toggle = () =&gt; setValue(v =&gt; !v);
  return [value, toggle];
}
C
function useToggle() {
  const [value, setValue] = useState(false);
  const toggle = () =&gt; setValue(!value);
  return [value, toggle];
}
D
function useToggle() {
  const [value, setValue] = useState(false);
  function toggle() {
    setValue(!value);
  }
  return { value, toggle };
}
Attempts:
2 left
💡 Hint
Remember to use the functional update form to avoid stale state.
🔧 Debug
advanced
2:00remaining
Why does this custom hook cause an infinite loop?
Examine this custom hook and component. Why does it cause an infinite render loop? function useData() { const [data, setData] = useState(null); fetch('https://api.example.com/data') .then(res => res.json()) .then(setData); return data; } function Component() { const data = useData(); return
{data ? data.message : 'Loading...'}
; }
ABecause the component returns null instead of JSX.
BBecause setData is called with a wrong argument type causing an error.
CBecause useState is used incorrectly without initial value.
DBecause fetch is called on every render inside the hook without useEffect, causing continuous state updates.
Attempts:
2 left
💡 Hint
Think about what happens when state updates inside a hook without controlling side effects.
state_output
advanced
2:00remaining
What is the final count displayed after clicking the button twice?
Given this component using a custom hook, what number will show after clicking the button two times? function useCounter() { const [count, setCount] = useState(0); function increment() { setCount(count + 1); setCount(count + 1); } return { count, increment }; } function Counter() { const { count, increment } = useCounter(); return ; }
ACount: 2
BCount: 0
CCount: 1
DCount: 4
Attempts:
2 left
💡 Hint
Remember how React batches state updates and how stale state values affect setState calls.
🧠 Conceptual
expert
2:00remaining
Which statement best explains why custom hooks must start with 'use'?
Why do React custom hooks have to start with the word 'use'?
ABecause React relies on the 'use' prefix to identify hooks and enforce rules of hooks during compilation and runtime.
BBecause it is a naming convention to help developers remember which functions are hooks, but React does not enforce it.
CBecause hooks starting with 'use' automatically get memoized by React for performance.
DBecause only functions starting with 'use' can access React context.
Attempts:
2 left
💡 Hint
Think about how React detects hooks to apply special rules.