0
0
Reactframework~20 mins

Why custom hooks are used in React - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Hooks Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Purpose of Custom Hooks in React

Why do developers create custom hooks in React?

ATo directly manipulate the DOM elements
BTo replace all class components with functional components
CTo reuse stateful logic across multiple components easily
DTo increase the size of the application bundle
Attempts:
2 left
💡 Hint

Think about how to avoid repeating the same code in different parts of your app.

component_behavior
intermediate
2:00remaining
Behavior of a Custom Hook

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?

AEach component can share the same fetching logic without rewriting it
BIt forces all components to fetch data at the same time
CIt disables React's built-in hooks like useState
DIt automatically caches data in the browser storage
Attempts:
2 left
💡 Hint

Think about how to avoid copying the same code for fetching data in every component.

📝 Syntax
advanced
2:00remaining
Correct Syntax for a Custom Hook

Which option shows the correct way to define a custom hook that returns a count and a function to increment it?

React
function useCounter() {
  const [count, setCount] = React.useState(0);
  const increment = () => setCount(count + 1);
  return { count, increment };
}
Afunction useCounter() { let count = 0; function increment() { count++; } return { count, increment }; }
Bfunction counterHook() { const [count, setCount] = React.useState(0); const increment = () => setCount(count + 1); return [count, increment]; }
Cfunction UseCounter() { const count = 0; function increment() { count++; } return { count, increment }; }
Dfunction useCounter() { const [count, setCount] = React.useState(0); const increment = () => setCount(count + 1); return { count, increment }; }
Attempts:
2 left
💡 Hint

Custom hooks must start with 'use' and use React hooks inside.

🔧 Debug
advanced
2:00remaining
Identifying Error in Custom Hook Usage

What error will occur if a custom hook is called inside a regular JavaScript function instead of a React component or another hook?

AReact Hook "useCustomHook" cannot be called inside a regular function
BTypeError: useCustomHook is not a function
CSyntaxError: Unexpected token 'useCustomHook'
DNo error, it works fine
Attempts:
2 left
💡 Hint

React hooks have rules about where they can be called.

state_output
expert
2:00remaining
State Behavior in Custom Hook Across Components

Given a custom hook that uses useState internally, what happens to the state when two different components use this hook?

ABoth components share the same state instance from the hook
BEach component has its own independent state from the hook
CThe state resets to initial value whenever a component re-renders
DThe hook state is global and updates all components simultaneously
Attempts:
2 left
💡 Hint

Think about how React hooks work inside components.