Complete the code to define a custom React hook that tracks window width.
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; }
Custom React hooks must start with use to follow naming conventions and enable React to identify them as hooks.
Complete the code to correctly name a hook that fetches user data.
function [1]() { const [user, setUser] = React.useState(null); React.useEffect(() => { fetch('/api/user') .then(res => res.json()) .then(data => setUser(data)); }, []); return user; }
Hooks must start with use. useUserData follows this rule and clearly describes the hook's purpose.
Fix the error in the hook name to follow React hook naming conventions.
function [1]() { const [count, setCount] = React.useState(0); return [count, setCount]; }
The hook name must start with use. useCount is the correct form.
Fill both blanks to correctly name a hook and its internal state variable.
function [1]() { const [[2], setValue] = React.useState(''); return [[2], setValue]; }
The hook name must start with use, so useInput is correct. The state variable should be descriptive, like inputValue.
Fill all three blanks to create a hook that manages a boolean toggle state with proper naming.
function [1]() { const [[2], [3]] = React.useState(false); const toggle = () => [3](![2]); return [[2], toggle]; }
The hook name useToggle follows conventions. The state variable isOn clearly shows a boolean. The setter setIsOn matches the state variable name.