React hooks must start with the word use. Why is this naming rule important?
Think about how React detects hooks inside components.
React relies on the 'use' prefix to identify hooks so it can apply rules like calling them only at the top level and inside React functions. This helps React manage state and effects correctly.
Choose the valid React hook name from the options below.
Remember hooks must start with 'use'.
Only names starting with 'use' are recognized as hooks by React. 'useDataFetch' follows this rule.
Consider a custom hook named fetchData (without 'use' prefix). What is the likely behavior when used inside a React component?
Think about how React enforces hook rules.
React identifies hooks by their 'use' prefix. Without it, React cannot enforce rules like calling hooks only at the top level, which can cause bugs or warnings.
Given this custom hook code, which naming mistake causes React to warn about invalid hook usage?
function fetchUserData() {
const [user, setUser] = React.useState(null);
// ...fetch logic
return user;
}Check the function name against React hook naming rules.
React expects custom hooks to start with 'use' to identify them properly. Naming it 'fetchUserData' breaks this rule and causes warnings.
Consider this React component and custom hook:
function fetchCount() {
const [count, setCount] = React.useState(0);
React.useEffect(() => {
const timer = setInterval(() => setCount(c => c + 1), 1000);
return () => clearInterval(timer);
}, []);
return count;
}
function Counter() {
const count = fetchCount();
return {count};
}What will happen when <Counter /> is rendered?
Consider React's hook rules and naming conventions.
The hook works but React warns because the function name does not start with 'use'. This breaks the hook naming convention and React's hook rules.