0
0
Reactframework~20 mins

Creating custom hooks in React - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom 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 the following React custom hook and component. What will be displayed when the component renders?

React
import React, { useState, useEffect } from 'react';

function useCounter(initial) {
  const [count, setCount] = useState(initial);
  useEffect(() => {
    const id = setInterval(() => setCount(c => c + 1), 1000);
    return () => clearInterval(id);
  }, []);
  return count;
}

function Counter() {
  const count = useCounter(0);
  return <div>{count}</div>;
}
AThe component displays 0 initially and increments by 1 every second.
BThe component displays 0 and never changes.
CThe component throws an error because hooks cannot be used inside functions.
DThe component displays NaN because the state is not initialized properly.
Attempts:
2 left
💡 Hint

Think about how useEffect with an empty dependency array works and how setInterval updates state.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a custom hook that returns window width?

Which of the following custom hooks correctly returns the current window width and updates on resize?

A
function useWindowWidth() {
  const [width, setWidth] = React.useState();
  window.addEventListener('resize', () =&gt; setWidth(window.innerWidth));
  return width;
}
B
function useWindowWidth() {
  const [width, setWidth] = React.useState(window.innerWidth);
  React.useEffect(() =&gt; {
    window.onresize = () =&gt; setWidth(window.innerWidth);
  }, []);
  return width;
}
C
function useWindowWidth() {
  const [width, setWidth] = React.useState(window.innerWidth);
  React.useEffect(() =&gt; {
    const handleResize = () =&gt; setWidth(window.innerWidth);
    window.addEventListener('resize', handleResize);
    return () =&gt; window.removeEventListener('resize', handleResize);
  }, []);
  return width;
}
D
function useWindowWidth() {
  const [width, setWidth] = React.useState(window.innerWidth);
  React.useEffect(() =&gt; {
    const handleResize = () =&gt; setWidth(window.innerWidth);
    window.addEventListener('resize', handleResize);
  }, []);
  return width;
}
Attempts:
2 left
💡 Hint

Remember to clean up event listeners in useEffect to avoid memory leaks.

🔧 Debug
advanced
2:00remaining
Why does this custom hook cause an infinite render loop?

Examine the custom hook below. Why does it cause an infinite render loop?

React
import { useState, useEffect } from 'react';

function useData() {
  const [data, setData] = useState(null);
  useEffect(() => {
    fetch('https://api.example.com/data')
      .then(res => res.json())
      .then(json => setData(json));
  }, [data]);
  return data;
}
ABecause the effect depends on 'data', it runs after every state update, causing continuous fetches.
BBecause 'setData' is missing from the dependency array, React warns about missing dependencies.
CBecause 'fetch' is asynchronous, it causes the component to crash.
DBecause 'useState' is called inside 'useEffect', which is invalid.
Attempts:
2 left
💡 Hint

Think about what triggers useEffect and how state updates affect dependencies.

state_output
advanced
2:00remaining
What is the value of 'count' after clicking the button twice quickly?

Given this custom hook and component, what will be the value of count after clicking the button twice quickly?

React
import React, { useState } from 'react';

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

function Counter() {
  const [count, increment] = useCounter();
  return <button onClick={increment}>{count}</button>;
}
AThe count increases by 4 after two quick clicks, showing 4.
BThe count increases by 1 after two quick clicks, showing 1.
CThe count increases by 2 after two quick clicks, showing 2.
DThe count increases by 3 after two quick clicks, showing 3.
Attempts:
2 left
💡 Hint

Remember that React state updates may be batched and that setCount(count + 1) uses the current closure value.

🧠 Conceptual
expert
2:00remaining
Why should custom hooks start with 'use' prefix?

Why is it important that custom React hooks start with the prefix use?

ABecause hooks without 'use' prefix will cause syntax errors in JSX files.
BBecause it is a naming convention that helps developers recognize hooks but has no effect on React behavior.
CBecause only functions starting with 'use' can access React context.
DBecause React relies on the 'use' prefix to identify hooks and enforce the rules of hooks during compilation and runtime.
Attempts:
2 left
💡 Hint

Think about how React detects hooks and enforces rules like calling hooks only at the top level.