Advertisement
Beginner React, Hooks

What are hooks in React and why were they introduced?

Short answer

Hooks are special functions, like useState and useEffect, that let a function component use features such as state and lifecycle behavior, which used to only be available in class components.

Save React mock interview
Advertisement
AModel answer
Hooks are special functions, like useState and useEffect, that let a function component use features such as state and lifecycle behavior, which used to only be available in class components. They were introduced to make components simpler to write and to make it easier to reuse logic between components.
function Counter() {
  const [count, setCount] = useState(0);
  return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
}
Real-world example
A team rewrites an old class based counter component into a much shorter function component using the useState hook, making the code easier to read and maintain.
Likely follow-ups
  • What problems did hooks solve that class components struggled with?
  • Can hooks be used inside a class component?
Related topics