Hooks & State Questions with Answers

31 practice questions on Hooks & State (React & Web) with answers and step-by-step solutions. Try 10 samples below, then practise all 31 free in the AptiNest app.

Sample questions

Question 1Easy

Which rule of hooks does this code break?

function App({ on }) {
  if (on) {
    const [n, setN] = useState(0);
  }
  return null;
}
  1. Hooks may only be used in class components
  2. useState must be given an object
  3. Hooks must not be called conditionally; call them at the top level every render
  4. A hook name must start with a capital letter

Answer: C. Hooks must not be called conditionally; call them at the top level every render

Explanation

React tracks hooks by call order, so they must run in the same order on every render, never inside conditions or loops.

Step-by-step solution

  1. Hooks are matched by order.
  2. A conditional hook changes the order.

Why the other options are wrong

  • A. Hooks are for function components.
  • B. Any value type is fine.
  • D. Hooks start with the prefix use.

Question 2Easy

What happens when you change ref.current for a ref created with useRef?

  1. The component re-renders with the new value
  2. The value changes, but no re-render is triggered
  3. React throws an error because refs are immutable
  4. The ref is reset to its initial value on next render

Answer: B. The value changes, but no re-render is triggered

Explanation

A ref is a mutable box that persists across renders. Changing it is not tracked by React, which makes it suitable for values that should not affect rendering.

Step-by-step solution

  1. Refs from useRef are plain mutable objects.
  2. Changing ref.current mutates the value directly.
  3. React does not track it, so no re-render happens.

Why the other options are wrong

  • A. Re-render happens with state, not refs.
  • C. Refs are mutable by design.
  • D. The ref is not reset.

Question 3Easy

The button is clicked once. What is logged?

function Box() {
  const [n, setN] = useState(0);
  return <button id="b" onClick={() => { setN(5); console.log(n); }}>{n}</button>;
}
  1. 5
  2. undefined
  3. Nothing is logged
  4. 0

Answer: D. 0

Explanation

n is a snapshot from this render, so the log still shows 0 although the state is set to 5.

Step-by-step solution

  1. setN(5) schedules a re-render.
  2. The current handler still sees n = 0.
  3. Logs 0.

Why the other options are wrong

  • A. State updates do not change the variable in the current render.
  • B. n is defined.
  • C. The handler logs n.

Tip: State is a snapshot per render.

Question 4Easy

What does useState return?

  1. A Promise that resolves to the state
  2. An object with get and set methods
  3. Only the current state value
  4. An array: [value, setValue]

Answer: D. An array: [value, setValue]

Explanation

const [value, setValue] = useState(initial). Calling setValue schedules a re-render with the new value.

Step-by-step solution

  1. useState(initial) returns a two-item array.
  2. The first item is the current value and the second is the setter function.
  3. So the correct answer is option D: [value, setValue].

Why the other options are wrong

  • A. UseState does not return a Promise; it returns the array right away.
  • B. It returns an array, not an object with get and set methods.
  • C. It returns the current value and a setter, not just the value.

Tip: Array destructuring names the pair: const [value, setValue] = useState(0).

Question 5Medium

The button is clicked once. What does it display afterwards?

import { useState } from "react";

function Counter() {
  const [count, setCount] = useState(0);
  function handleClick() {
    setCount(c => c + 1);
    setCount(c => c + 1);
  }
  return <button onClick={handleClick}>{count}</button>;
}
  1. 4
  2. 1
  3. 2
  4. 0

Answer: C. 2

Explanation

Updater functions are queued and each receives the result of the previous one: 0 to 1, then 1 to 2.

Step-by-step solution

  1. Each call setCount(c => c + 1) is an updater function.
  2. React queues both updaters and applies them in order to the latest value.
  3. 0 becomes 1, then 1 becomes 2.
  4. The button shows 2.

Why the other options are wrong

  • A. 4 would need doubling twice; updates add 1 each.
  • B. 1 happens with setCount(count + 1) twice, which uses a stale count.
  • D. 0 ignores that the state update ran.

Tip: Use the updater form when the new state depends on the old one.

Question 6Medium

The button is clicked once. What does it show?

function reducer(state, action) {
  switch (action.type) {
    case 'inc': return { n: state.n + 1 };
    case 'dec': return { n: state.n - 1 };
    default: return state;
  }
}
function App() {
  const [s, dispatch] = useReducer(reducer, { n: 0 });
  return <button id="b" onClick={() => { dispatch({ type: 'inc' }); dispatch({ type: 'inc' }); dispatch({ type: 'dec' }); dispatch({ type: 'noop' }); }}>{s.n}</button>;
}
  1. 1
  2. 2
  3. 0
  4. 3

Answer: A. 1

Explanation

The actions run in order: +1, +1, -1, then noop returns the state unchanged, so n = 1.

Step-by-step solution

  1. 0 -> 1 -> 2 -> 1.
  2. noop leaves the state alone.
  3. Result 1.

Why the other options are wrong

  • B. The decrement reduces the total by one.
  • C. Two increments and one decrement do not cancel out.
  • D. Only inc and dec change the value.

Question 7Medium

The button is clicked twice. What does it show?

function App() {
  const [list, setList] = useState([1, 2]);
  return <button id="b" onClick={() => setList([...list, list.length + 1])}>{list.join('-')}</button>;
}
  1. 1-2
  2. 1-2-2
  3. 3
  4. 1-2-3-4

Answer: D. 1-2-3-4

Explanation

Spread copies the old items and appends the new length-based value; two clicks give 1-2-3-4.

Step-by-step solution

  1. Click 1: [1,2,3].
  2. Click 2: [1,2,3,4].

Why the other options are wrong

  • A. The new array triggers a re-render.
  • B. The new value is list.length + 1 = 3.
  • C. The old items are kept via spread.

Question 8Medium

Two components both call the same custom hook useCounter(). What happens to the state?

  1. Each component gets its own independent state
  2. They share one state value
  3. The second call overwrites the first
  4. React throws an error about duplicate hooks

Answer: A. Each component gets its own independent state

Explanation

A custom hook is a reusable function; every call creates its own state slots in the calling component.

Step-by-step solution

  1. Each call to useState inside creates new state.
  2. State is per component instance.

Why the other options are wrong

  • B. Hooks share logic, not state.
  • C. There is no overwriting.
  • D. Using a hook in many components is the whole purpose.

Question 9Hard

<App /> is mounted and the button is clicked once. What is logged?

function App() {
  const [n, setN] = useState(0);
  const [m, setM] = useState(0);
  console.log('render');
  return <button id="b" onClick={() => { setN(n + 1); setM(m + 1); }}>{n + m}</button>;
}
  1. render, render, render
  2. render
  3. render, render, render, render
  4. render, render

Answer: D. render, render

Explanation

Two state updates in one event handler are batched, so the click causes just one re-render: render on mount, render after click.

Step-by-step solution

  1. Mount: render.
  2. Click: two setters batched.
  3. One more render.

Why the other options are wrong

  • A. Updates in one event handler are batched into one render.
  • B. Mount renders once and the click renders again.
  • C. Only two renders happen.

Question 10Hard

The component renders once and the button is then clicked twice. How many times is "init" logged?

import { useState } from "react";

function init() {
  console.log("init");
  return 0;
}

function Counter() {
  const [count, setCount] = useState(init());
  return <button onClick={() => setCount(count + 1)}>{count}</button>;
}
  1. 0
  2. 1
  3. 2
  4. 3

Answer: D. 3

Explanation

init() is an ordinary call that runs on every render (3 renders), even though React uses its result only the first time. Pass the function itself, useState(init), to run it once.

Step-by-step solution

  1. init() is called in the useState argument, so it runs on every render of Counter.
  2. The component renders once on mount, then re-renders after each of the two clicks.
  3. So init runs on 3 renders, and "init" is logged 3 times.
  4. The correct answer is option D.

Why the other options are wrong

  • A. Init is called at least once on the first render, so it logs at least once.
  • B. There are three renders in total, so init is logged more than once.
  • C. The mount plus two click renders make 3 calls, not 2.

Tip: Pass a function to useState (useState(init)) so it runs only on the first render.

Practise all 31 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all React & Web topics · All topics