0
0
Reactframework~20 mins

Custom hook best practices in React - Practice Problems & Coding Challenges

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 this React custom hook that tracks window width. What will the component display after resizing the window to 800 pixels wide?

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

function useWindowWidth() {
  const [width, setWidth] = useState(window.innerWidth);

  useEffect(() => {
    function handleResize() {
      setWidth(window.innerWidth);
    }
    window.addEventListener('resize', handleResize);
    return () => window.removeEventListener('resize', handleResize);
  }, []);

  return width;
}

export default function WidthDisplay() {
  const width = useWindowWidth();
  return <div>Window width is {width}</div>;
}
AWindow width is initial window width before resize
BWindow width is 800
CWindow width is undefined
DComponent crashes with error
Attempts:
2 left
💡 Hint

Think about how the hook updates state on window resize events.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a custom hook that fetches data?

Which of the following custom hook definitions is syntactically correct and follows React hook naming conventions?

Afunction UseFetchData(url) { const [data, setData] = useState(null); useEffect(() => { fetch(url).then(res => res.json()).then(setData); }, [url]); return data; }
Bfunction fetchDataHook(url) { const [data, setData] = useState(null); useEffect(() => { fetch(url).then(res => res.json()).then(setData); }, [url]); return data; }
Cfunction useFetchData(url) { const [data, setData] = useState(null); useEffect(() => { fetch(url).then(res => res.json()).then(setData); }, [url]); return data; }
Dfunction usefetchdata(url) { const [data, setData] = useState(null); useEffect(() => { fetch(url).then(res => res.json()).then(setData); }, [url]); return data; }
Attempts:
2 left
💡 Hint

Custom hooks must start with 'use' and use camelCase.

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

Examine this custom hook. Why does the component using it keep re-rendering infinitely?

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

function useCounter() {
  const [count, setCount] = useState(0);

  useEffect(() => {
    setCount(count + 1);
  }, [count]);

  return count;
}
ABecause setCount inside useEffect updates count, triggering useEffect again endlessly
BBecause useState is called inside useEffect causing re-renders
CBecause useEffect has no dependency array causing infinite calls
DBecause count is never updated, so useEffect never stops
Attempts:
2 left
💡 Hint

Think about what happens when state changes inside useEffect with that dependency.

🧠 Conceptual
advanced
2:00remaining
What is a key best practice when creating custom hooks?

Which of the following is a best practice when writing custom React hooks?

ACustom hooks should start with 'use' and can encapsulate stateful logic and side effects
BCustom hooks should only call other hooks and never contain side effects
CCustom hooks must always return JSX elements to be reusable
DCustom hooks should be class components to manage lifecycle methods
Attempts:
2 left
💡 Hint

Think about what custom hooks are designed to do in React.

state_output
expert
3:00remaining
What is the final value of count after this custom hook runs?

Given this custom hook and component, what is the final value of count displayed?

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

function useDelayedIncrement(initial) {
  const [count, setCount] = useState(initial);

  useEffect(() => {
    const id = setTimeout(() => {
      setCount(c => c + 1);
    }, 1000);
    return () => clearTimeout(id);
  }, [count]);

  return count;
}

export default function Counter() {
  const count = useDelayedIncrement(0);
  return <div>{count}</div>;
}
AThe component crashes with a memory leak error
BThe count increases by 1 only once after 1 second
CThe count stays at 0 forever
DThe count increases by 1 every second indefinitely
Attempts:
2 left
💡 Hint

Consider how the effect depends on count and triggers a timeout each time.