0
0
Reactframework~10 mins

Hook naming conventions in React - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a custom React hook that tracks window width.

React
function [1]() {
  const [width, setWidth] = React.useState(window.innerWidth);
  React.useEffect(() => {
    const handleResize = () => setWidth(window.innerWidth);
    window.addEventListener('resize', handleResize);
    return () => window.removeEventListener('resize', handleResize);
  }, []);
  return width;
}
Drag options to blanks, or click blank then click option'
AgetWindowWidth
BWindowWidthHook
CuseWindowWidth
DwindowWidth
Attempts:
3 left
💡 Hint
Common Mistakes
Naming the hook without the 'use' prefix
Using camelCase but missing 'use' at the start
2fill in blank
medium

Complete the code to correctly name a hook that fetches user data.

React
function [1]() {
  const [user, setUser] = React.useState(null);
  React.useEffect(() => {
    fetch('/api/user')
      .then(res => res.json())
      .then(data => setUser(data));
  }, []);
  return user;
}
Drag options to blanks, or click blank then click option'
AfetchUserData
BuseUserData
CgetUser
DUserDataHook
Attempts:
3 left
💡 Hint
Common Mistakes
Using verbs like 'fetch' or 'get' without 'use' prefix
Naming the hook like a regular function
3fill in blank
hard

Fix the error in the hook name to follow React hook naming conventions.

React
function [1]() {
  const [count, setCount] = React.useState(0);
  return [count, setCount];
}
Drag options to blanks, or click blank then click option'
AuseCount
BCountHook
CcountState
DgetCount
Attempts:
3 left
💡 Hint
Common Mistakes
Naming hooks like regular functions without 'use' prefix
Using camelCase but missing 'use' at the start
4fill in blank
hard

Fill both blanks to correctly name a hook and its internal state variable.

React
function [1]() {
  const [[2], setValue] = React.useState('');
  return [[2], setValue];
}
Drag options to blanks, or click blank then click option'
AuseInput
BinputValue
Cvalue
DInputHook
Attempts:
3 left
💡 Hint
Common Mistakes
Naming the hook without 'use' prefix
Using generic or unclear state variable names
5fill in blank
hard

Fill all three blanks to create a hook that manages a boolean toggle state with proper naming.

React
function [1]() {
  const [[2], [3]] = React.useState(false);
  const toggle = () => [3](![2]);
  return [[2], toggle];
}
Drag options to blanks, or click blank then click option'
AuseToggle
BisOn
CsetIsOn
DtoggleState
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent names for state and setter
Not starting hook name with 'use'