Rendering & Keys Questions with Answers

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

Sample questions

Question 1Easy

<Parent /> is mounted and the button is clicked once. How many times is "child" logged? (Each log is separated by commas.)

function Child() { console.log('child'); return null; }
function Parent() {
  const [n, setN] = useState(0);
  return <section><button id="b" onClick={() => setN(n + 1)}>+</button><Child /></section>;
}
  1. child, child
  2. child
  3. child, child, child
  4. Nothing is logged

Answer: A. child, child

Explanation

A parent re-render re-renders its children by default: once on mount and once after the click.

Step-by-step solution

  1. Mount renders Child: child.
  2. Click re-renders Parent and Child: child.

Why the other options are wrong

  • B. The child re-renders with its parent after the click.
  • C. Only one click occurred.
  • D. Child logs on mount.

Question 2Easy

Why does React need a key prop on elements rendered from a list?

  1. To let CSS style each item in the list individually
  2. To match items between renders so state stays with the right one
  3. Keys are optional hints and have no effect on the output
  4. To make React sort the items by key before rendering them

Answer: B. To match items between renders so state stays with the right one

Explanation

Keys let React match items across renders when items are added, removed or reordered. They must be stable and unique among siblings.

Step-by-step solution

  1. React compares old and new lists on each render.
  2. Keys give each item a stable identity.
  3. Matching keys let React keep state and DOM with the right item when items move or change.

Why the other options are wrong

  • A. Keys are not exposed to CSS.
  • C. Keys affect reconciliation and can change behaviour (state).
  • D. React does not sort by key.

Tip: Use ids, not indexes, as keys.

Question 3Easy

<Msg count={0} /> is rendered. What text appears inside the heading?

function Msg({ count }) {
  return <h2>{count && <em>items</em>}</h2>;
}
  1. Nothing
  2. items
  3. false
  4. 0

Answer: D. 0

Explanation

The && returns 0 when count is 0, and React renders the number 0.

Step-by-step solution

  1. 0 && x evaluates to 0.
  2. React renders numbers.
  3. Output: 0.

Why the other options are wrong

  • A. 0 is rendered as the text "0".
  • B. count is 0, which is falsy.
  • C. A falsy 0 renders as 0, not false.

Tip: Use count > 0 && ... or a ternary.

Question 4Easy

Why does React ask for a key prop when rendering a list?

  1. So it can track which items changed between renders
  2. To style each list item differently
  3. To pass the item as a prop to the child
  4. To speed up the first render only

Answer: A. So it can track which items changed between renders

Explanation

Stable keys let React match old and new items so it reuses the right elements and state.

Step-by-step solution

  1. Keys identify items.
  2. React diffs by key.

Why the other options are wrong

  • B. Keys are not for styling.
  • C. key is not available as a prop.
  • D. Keys matter for updates.

Question 5Medium

A parent component re-renders. By default, what happens to a child component whose props did not change?

  1. It re-renders only if it uses state
  2. It re-renders too
  3. It is skipped automatically
  4. It unmounts

Answer: B. It re-renders too

Explanation

By default React re-renders the whole subtree. Wrapping the child in memo() lets React skip it when its props are shallowly equal.

Step-by-step solution

  1. By default, React re-renders all children of a re-rendered parent.
  2. Props being unchanged does not stop it.
  3. Only React.memo (or similar) skips this.

Why the other options are wrong

  • A. State is not required for the child to re-render.
  • C. Skipping needs React.memo.
  • D. Re-rendering does not unmount it.

Tip: Parent renders, children render, unless memoised.

Question 6Medium

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

const Child = memo(function Child() { console.log('child'); return null; });
function Parent() {
  const [n, setN] = useState(0);
  return <section><button id="b" onClick={() => setN(n + 1)}>{n}</button><Child /></section>;
}
  1. child, child
  2. Nothing is logged
  3. child, child, child
  4. child

Answer: D. child

Explanation

memo skips the child re-render because it receives no changing props, leaving only the mount render.

Step-by-step solution

  1. Mount: child.
  2. Click re-renders Parent.
  3. memo sees the same props and skips Child.

Why the other options are wrong

  • A. memo skips re-renders when props are unchanged.
  • B. Child renders on mount.
  • C. Only one click occurred.

Question 7Medium

Why is using the array index as key risky in a list where items can be inserted or reordered?

  1. Using indexes as keys causes a compile error in JSX files
  2. After a reorder, an index can refer to a different item
  3. It forces every item in the list to render twice per update
  4. React does not allow numbers to be used as key values at all

Answer: B. After a reorder, an index can refer to a different item

Explanation

Indexes are only safe for static lists. Prefer a stable ID taken from the data.

Step-by-step solution

  1. A key identifies an item across renders so React can match it to the right element.
  2. An index is a position, and after an insert or reorder the same index can point to a different item.
  3. So React may reuse the wrong element's state, which is option B.

Why the other options are wrong

  • A. Index keys are valid JSX; they do not cause a compile error.
  • C. Index keys do not force items to render twice per update.
  • D. Numbers are allowed as keys; React converts them to strings.

Tip: Use a stable ID from the data as the key; indexes are fine only for static lists.

Question 8Medium

<App k={1} /> is mounted, "x" is typed in the input, then the app re-renders with k={2}. What does JSON.stringify(input.props.value) print?

function Field() {
  const [t, setT] = useState('');
  return <input id="i" value={t} onChange={e => setT(e.target.value)} />;
}
function App({ k }) { return <Field key={k} />; }
  1. x
  2. ""
  3. Error
  4. undefined

Answer: B. ""

Explanation

A new key remounts Field, resetting its state to the empty string.

Step-by-step solution

  1. Typing sets state to "x".
  2. Key change unmounts and remounts Field.
  3. State is empty again.

Why the other options are wrong

  • A. Changing the key remounts the component.
  • C. Changing keys is allowed.
  • D. The input has a defined value.

Question 9Hard

<List items={["a","b"]} /> is mounted and re-rendered with items ["b"]. What text does the list show?

function Item({ label }) {
  const [val] = useState(label);
  return <li>{val}</li>;
}
function List({ items }) {
  return <ul>{items.map(x => <Item key={x} label={x} />)}</ul>;
}
  1. b
  2. a
  3. ab
  4. a, b

Answer: A. b

Explanation

Keys identify items, so the "b" item keeps its own state and "a" is unmounted.

Step-by-step solution

  1. Keys are a and b.
  2. Removing "a" unmounts only that item.
  3. "b" keeps its state.

Why the other options are wrong

  • B. The old state is only kept for the matching key.
  • C. "a" was removed.
  • D. The first item was removed.

Question 10Hard

<App flag={true} /> is mounted, the button is clicked once, then the app re-renders with flag={false}. What does the button show?

function A() { const [n, setN] = useState(0); return <button id="b" onClick={() => setN(n + 1)}>{n}</button>; }
function B() { const [n, setN] = useState(0); return <button id="b" onClick={() => setN(n + 1)}>{n}</button>; }
function App({ flag }) {
  return flag ? <A /> : <B />;
}
  1. 0
  2. 1
  3. B1
  4. 2

Answer: A. 0

Explanation

A and B are different component types, so React unmounts A and mounts B with fresh state, showing 0.

Step-by-step solution

  1. Click sets A state to 1.
  2. Switching types unmounts A.
  3. B mounts with n = 0.

Why the other options are wrong

  • B. Different component types at the same position are remounted.
  • C. The component does not print its name.
  • D. The click happened only once.

Practise all 18 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