React Components Questions with Answers

18 practice questions on React Components (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

<List /> is rendered. What text does it show?

function Row({ label, value }) {
  return <li>{label}: {value}</li>;
}
function List() {
  const data = { label: 'Age', value: 21 };
  return <ul><Row {...data} /></ul>;
}
  1. label: value
  2. Age: 21
  3. Age: undefined
  4. Age:

Answer: B. Age: 21

Explanation

The spread passes each key of data as a prop, so label is "Age" and value is 21.

Step-by-step solution

  1. {...data} expands to label and value props.
  2. Row renders "Age: 21".

Why the other options are wrong

  • A. Props are values, not names.
  • C. The spread passes both props.
  • D. The spread includes value as well.

Question 2Easy

Why must a custom React component name start with a capital letter, as in <UserCard />?

  1. It is only a style convention and has no effect on rendering
  2. Lowercase names are reserved for class components in React
  3. Capitalised components are memoised and so render faster
  4. JSX treats lowercase tags as HTML elements, not your component

Answer: D. JSX treats lowercase tags as HTML elements, not your component

Explanation

JSX compiles <div> to the string "div" but <UserCard> to a reference to the UserCard variable. A lowercase name is treated as an HTML tag.

Step-by-step solution

  1. JSX turns a lowercase tag name such as div into the string "div", which React treats as an HTML element.
  2. JSX turns a capitalised tag name such as UserCard into a reference to the variable UserCard.
  3. So a capital letter tells JSX to use your component instead of an HTML element.
  4. The correct answer is option D.

Why the other options are wrong

  • A. The capital letter changes how JSX compiles the tag, so it has an effect.
  • B. Lowercase names are not reserved for class components.
  • C. Capitalised names do not make a component memoised or faster.

Tip: Lowercase JSX tags mean HTML elements; capitalised tags mean components.

Question 3Easy

Can a component modify the props it receives (for example props.title = "x")?

  1. Yes, props are ordinary variables owned by the child
  2. Only when the prop's value is a string or number
  3. No, treat props as read-only; use state for changes
  4. Only in class components, through this.props

Answer: C. No, treat props as read-only; use state for changes

Explanation

Props are owned by the parent. A component that needs changing data keeps it in state or asks the parent to change it through a callback prop.

Step-by-step solution

  1. Props belong to the parent, and the child receives them as read-only values.
  2. Changing props.title inside the child breaks this rule and can cause hard-to-trace bugs.
  3. Data that the component must change belongs in state, or the parent changes it through a callback prop.
  4. So the correct answer is option C.

Why the other options are wrong

  • A. The child does not own its props; the parent does.
  • B. The type of the value does not decide whether props can be changed; props are read-only for every type.
  • D. Class components do not allow changing props either; this.props is read-only as well.

Tip: Treat props as read-only; use state for values that change inside the component.

Question 4Easy

<Greet /> is rendered with no props. What text does it show?

function Greet({ name = 'Guest' }) {
  return <h2>Hello, {name}</h2>;
}
  1. Hello, undefined
  2. Hello,
  3. Hello, Guest
  4. Hello, name

Answer: C. Hello, Guest

Explanation

The destructuring default "Guest" is used because name is undefined.

Step-by-step solution

  1. No name prop is passed.
  2. undefined triggers the default.
  3. Output: Hello, Guest.

Why the other options are wrong

  • A. Defaults apply when the prop is undefined.
  • B. The default fills the missing prop.
  • D. name is a variable, not literal text.

Question 5Medium

<App /> is rendered. What text does it show (no separators)?

const Ctx = createContext('root');
function Reader() { return <strong>{useContext(Ctx)}</strong>; }
function App() {
  return (
    <Ctx.Provider value="A">
      <Reader />
      <Ctx.Provider value="B">
        <Reader />
      </Ctx.Provider>
    </Ctx.Provider>
  );
}
  1. AA
  2. rootroot
  3. AB
  4. BB

Answer: C. AB

Explanation

A component reads the value of its closest Provider: the first Reader is under A and the second under the nested B.

Step-by-step solution

  1. First Reader: closest Provider is A.
  2. Second Reader: nested Provider B.
  3. Output: AB.

Why the other options are wrong

  • A. The inner provider overrides the value for its subtree.
  • B. Both readers are inside providers.
  • D. The first Reader is outside the inner provider.

Question 6Medium

What text does <Cart items={[]} /> render?

function Cart({ items }) {
  return <div>{items.length && <p>You have items</p>}</div>;
}
  1. You have items
  2. 0
  3. false
  4. Nothing (an empty div)

Answer: B. 0

Explanation

items.length is 0, so && returns 0. React does not render false, null or undefined, but it does render the number 0. Use items.length > 0 && ... instead.

Step-by-step solution

  1. Items is [], so items.length is 0.
  2. 0 && (a paragraph element) evaluates to 0, because && returns the first falsy value.
  3. React does not render false, null or undefined, but it does render the number 0.
  4. So the output is the text 0, which is option B.

Why the other options are wrong

  • A. The paragraph is not shown, because the && short-circuits on 0.
  • C. False would render nothing; here the falsy value is the number 0, which React prints.
  • D. The div is not empty; the number 0 is shown inside it.

Tip: Write items.length > 0 && ... to get a boolean, not a number, on the left of &&.

Question 7Medium

<Show items={[1, 2]} /> is rendered. What text does it show (all text nodes joined with no separator)?

function Show({ items }) {
  if (!items.length) return <h2>Empty</h2>;
  return <ul>{items.map(i => <li key={i}>{i}</li>)}</ul>;
}
  1. Empty
  2. 1, 2
  3. 12
  4. [1,2]

Answer: C. 12

Explanation

The array is not empty, so the list renders its items one after another.

Step-by-step solution

  1. items has two elements.
  2. The early return is skipped.
  3. Text content: 12.

Why the other options are wrong

  • A. An array with elements renders the list.
  • B. Text nodes are concatenated without separators.
  • D. The list items render as text.

Question 8Medium

<App /> is mounted. Assume a click on the button is simulated if a handler exists. What is logged in total?

function Btn({ onClick }) {
  return <button id="b" onClick={onClick}>go</button>;
}
function App() {
  const hit = () => console.log('hit');
  return <Btn onClick={hit()} />;
}
  1. hit, hit
  2. hit
  3. Nothing is logged
  4. TypeError

Answer: B. hit

Explanation

onClick={hit()} runs hit during render and passes its undefined result as the handler, so there is one log from rendering and the click does nothing.

Step-by-step solution

  1. hit() executes during render: logs once.
  2. The handler passed down is undefined.
  3. Clicking does nothing more.

Why the other options are wrong

  • A. The click does not call anything, because the handler is undefined.
  • C. hit() is called while rendering.
  • D. Clicking with an undefined handler is a no-op in this harness.

Tip: Pass a function: onClick={hit} or onClick={() => hit()}.

Question 9Hard

<Probe>{["a", null, "b"]}</Probe> is rendered. What number does it show?

function Probe({ children }) {
  return <h2>{React.Children.count(children)}</h2>;
}
  1. 2
  2. 3
  3. 4
  4. 1

Answer: B. 3

Explanation

Children.count counts strings, numbers, elements and empty nodes; arrays are flattened, so the items are a, null and b.

Step-by-step solution

  1. The array is flattened.
  2. Items: a, null, b.
  3. Three nodes are counted.

Why the other options are wrong

  • A. Empty nodes such as null still count as individual nodes.
  • C. The array itself is not counted in addition to its items.
  • D. Each child is counted.

Question 10Hard

In React 19, what does this log after <Form /> renders?

import { useRef, useEffect } from "react";

function MyInput({ ref }) {
  return <input ref={ref} />;
}

function Form() {
  const inputRef = useRef(null);
  useEffect(() => {
    console.log(inputRef.current.tagName);
  }, []);
  return <MyInput ref={inputRef} />;
}
  1. null
  2. A TypeError, because forwardRef is required
  3. undefined
  4. INPUT

Answer: D. INPUT

Explanation

Since React 19, function components receive ref as a regular prop, so forwardRef is no longer needed. The ref is attached to the input before effects run.

Step-by-step solution

  1. In React 19, ref is passed to MyInput as a normal prop, so forwardRef is not needed.
  2. MyInput puts that ref on the input element.
  3. Form's effect runs after the commit, so inputRef.current is the input DOM node.
  4. TagName of an input is "INPUT", so the log shows INPUT, which is option D.

Why the other options are wrong

  • A. The ref is attached before the effect runs, so it is not null.
  • B. No error happens; React 19 accepts ref as a prop.
  • C. The ref is set, so the value is not undefined; tagName is the string "INPUT".

Tip: In React 19, read ref from props for function components; forwardRef is optional now.

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