Why do developers create custom hooks in React?
Think about how to avoid repeating the same code in different parts of your app.
Custom hooks let you share logic that uses React state and effects between components without repeating code.
Consider a custom hook that fetches data and returns loading status and data. What is the main benefit of using this hook in multiple components?
Think about how to avoid copying the same code for fetching data in every component.
Custom hooks encapsulate logic like data fetching so multiple components can use it without duplicating code.
Which option shows the correct way to define a custom hook that returns a count and a function to increment it?
function useCounter() { const [count, setCount] = React.useState(0); const increment = () => setCount(count + 1); return { count, increment }; }
Custom hooks must start with 'use' and use React hooks inside.
Option D correctly defines a custom hook using React's useState and returns an object with count and increment function.
What error will occur if a custom hook is called inside a regular JavaScript function instead of a React component or another hook?
React hooks have rules about where they can be called.
React enforces that hooks can only be called inside React function components or other hooks. Calling them elsewhere causes an error.
Given a custom hook that uses useState internally, what happens to the state when two different components use this hook?
Think about how React hooks work inside components.
Each call to a custom hook creates a separate state instance for that component. States are not shared unless explicitly designed.