React State and Props


React State and Props Interview with follow-up questions

1. What are state and props in React?

Both hold data that drives rendering, but they differ in ownership and mutability:

  • State – data owned and managed by the component itself; it's mutable over time and changing it triggers a re-render. In function components you create it with hooks:
  const [count, setCount] = useState(0);
  setCount(count + 1); // schedules a re-render

(In legacy classes, state lives in this.state and updates via this.setState.)

  • Props – data passed in from a parent; they're read-only from the child's perspective. The child renders based on them but cannot change them; the parent updates props by re-rendering with new values.

Mental model: props are arguments to a component, state is its private memory. Lift state up to a common ancestor (or a store/Context) when multiple components need to share it.

↑ Back to top

Follow-up 1

How do state and props differ?

State and props differ in the way they are used and managed in React:

  • State is managed internally by the component itself, while props are passed from a parent component to its child components.
  • State is mutable and can be changed using the setState() method, while props are read-only and cannot be changed by the child component.
  • State is used to store data that can change over time and affect the component's rendering, while props are used to pass data and methods from one component to another.

Follow-up 2

Can you provide an example of how to use state in a component?

Sure! Here's an example of a simple React component that uses state:

import React, { Component } from 'react';

class Counter extends Component {
  constructor(props) {
    super(props);
    this.state = {
      count: 0
    };
  }

  incrementCount() {
    this.setState({
      count: this.state.count + 1
    });
  }

  render() {
    return (
      <div>
        <p>Count: {this.state.count}</p>
         this.incrementCount()}&gt;Increment
      </div>
    );
  }
}

export default Counter;

In this example, the Counter component has a state property called count which is initially set to 0. The incrementCount() method is used to update the count state by incrementing it by 1. The current value of count is displayed in the component's render method.

Follow-up 3

Can you provide an example of how to use props in a component?

Certainly! Here's an example of a parent component passing props to a child component:

import React from 'react';

const ParentComponent = () =&gt; {
  const name = 'John Doe';
  const age = 25;

  return (

  );
};

const ChildComponent = (props) =&gt; {
  return (
    <div>
      <p>Name: {props.name}</p>
      <p>Age: {props.age}</p>
    </div>
  );
};

export default ParentComponent;

In this example, the ParentComponent passes two props (name and age) to the ChildComponent. The ChildComponent receives these props as an object (props) and can access their values using dot notation (props.name and props.age). The values of name and age are then rendered in the ChildComponent.

Follow-up 4

What is the significance of state and props in React?

State and props are significant in React because they enable the creation of dynamic and reusable components:

  • State allows components to manage and update their own data. It enables components to be aware of changes and re-render accordingly. State is useful for creating interactive and dynamic user interfaces.
  • Props facilitate the passing of data and methods between components. They allow components to be reusable and modular. Props make it easy to create parent-child relationships between components and enable the composition of complex UIs.

By using state and props effectively, React components can be built to be flexible, maintainable, and scalable.

2. How do you pass props from a parent component to a child component?

Set them as attributes on the child's JSX element; the child reads them from its props argument:

function Parent() {
  return  {}} /&gt;;
}

function Child({ message, count, onDone }) {
  return <p>{message} ({count})</p>;
}

Notes:

  • Props can be any value — strings, numbers, objects, arrays, and functions (used for child→parent communication).
  • Use the spread operator to forward many props: ``.
  • The content between tags is passed as the special children prop:
  <h1>Title</h1>
  // function Card({ children }) { return <div>{children}</div>; }

Props flow one way (parent → child) and are read-only in the child.

↑ Back to top

Follow-up 1

What happens if you don't pass any props?

If you don't pass any props from a parent component to a child component, the child component will still render, but the props object within the child component will be empty. This means that you won't be able to access any props passed from the parent component.

Follow-up 2

Can you pass multiple props at once?

Yes, you can pass multiple props at once from a parent component to a child component. Simply include multiple prop attributes when rendering the child component, each with a unique name and value.

For example, if you have a parent component called Parent and a child component called Child, you can pass multiple props to the child like this:




Within the Child component, you can access each prop using props.prop1, props.prop2, and props.prop3.

Follow-up 3

Can you provide an example of passing props from a parent to a child component?

Sure! Here's an example of passing props from a parent component to a child component:

// Parent.js
import React from 'react';
import Child from './Child';

function Parent() {
  const message = "Hello, World!";

  return (
    <div>
      <h1>Parent Component</h1>

    </div>
  );
}

export default Parent;

// Child.js
import React from 'react';

function Child(props) {
  return (
    <div>
      <h2>Child Component</h2>
      <p>{props.message}</p>
    </div>
  );
}

export default Child;

In this example, the Parent component passes a prop called message to the Child component. The Child component then displays the value of the message prop within a paragraph element.

3. What is the use of the setState function in React?

setState is the class-component API for updating state. Calling it merges the change into this.state and tells React to re-render:

this.setState({ count: this.state.count + 1 });
// functional form when next state depends on previous:
this.setState((prev) =&gt; ({ count: prev.count + 1 }));

Important: setState is asynchronous/batched — React may batch multiple updates in one render, so don't read this.state immediately after and expect the new value; use the updater function or componentDidUpdate.

In modern function components you don't use setState; you use the useState hook, whose setter behaves similarly (asynchronous, batched, supports an updater function):

const [count, setCount] = useState(0);
setCount((prev) =&gt; prev + 1);

So setState is still valid in class code, but new React uses useState (or useReducer for complex state).

↑ Back to top

Follow-up 1

What happens if you try to change the state directly without using setState?

If you try to change the state directly without using the setState function, the component will not re-render and the UI will not be updated to reflect the new state. This is because React relies on the setState function to track state changes and trigger the re-rendering process. Directly modifying the state will bypass this mechanism and lead to inconsistencies in the UI.

Follow-up 2

Can you provide an example of using setState?

Sure! Here's an example of using setState to update the state of a counter component:

import React, { Component } from 'react';

class Counter extends Component {
  constructor(props) {
    super(props);
    this.state = {
      count: 0
    };
  }

  incrementCount = () =&gt; {
    this.setState({
      count: this.state.count + 1
    });
  }

  render() {
    return (
      <div>
        <p>Count: {this.state.count}</p>
        Increment
      </div>
    );
  }
}

export default Counter;

In this example, the state of the counter component is stored in the count property. The incrementCount method is used to update the state by calling setState with the new count value.

Follow-up 3

What is the significance of the callback function in setState?

The callback function in setState is an optional parameter that can be used to perform additional actions after the state has been updated. It is executed after the state has been successfully updated and the component has been re-rendered. The callback function is useful when you need to perform some action that depends on the updated state, such as making an API call or updating the DOM based on the new state. Here's an example:

this.setState({
  count: this.state.count + 1
}, () =&gt; {
  console.log('State has been updated');
});

In this example, the callback function logs a message to the console after the state has been updated and the component has been re-rendered.

4. What is the difference between state and props in terms of their functionality?

Both manage data for rendering, but with different roles:

  • Stateinternal data a component owns and can change. Created with useState/useReducer (or this.state in classes). Mutating it (via the setter) re-renders the component. Use it for things that change over time due to user interaction or async results: form input, toggles, fetched data.

  • Props – data passed in from a parent, read-only in the receiving component. Used to configure a component and pass data/callbacks down. A child can't modify its props; only the parent can, by re-rendering with new values.

State Props
Owner the component itself the parent
Mutable? yes (via setter) no (read-only)
Purpose private, changing data configuration passed down

Together they enable reusable components (props) with their own behaviour (state).

↑ Back to top

Follow-up 1

Can you provide an example where you would use state instead of props?

Sure! Let's say we have a Counter component that needs to keep track of a count value and increment it when a button is clicked. In this case, we would use state to manage the count value within the Counter component. Here's an example:

import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  const incrementCount = () =&gt; {
    setCount(count + 1);
  };

  return (
    <div>
      <p>Count: {count}</p>
      Increment
    </div>
  );
}

Follow-up 2

Can you provide an example where you would use props instead of state?

Certainly! Let's consider a User component that displays user information such as name, age, and email. The user data is passed to the User component as props from a parent component. Here's an example:

import React from 'react';

function User(props) {
  return (
    <div>
      <p>Name: {props.name}</p>
      <p>Age: {props.age}</p>
      <p>Email: {props.email}</p>
    </div>
  );
}

// Usage:

Follow-up 3

What are the best practices for using state and props in React?

Here are some best practices for using state and props in React:

  • State:

    • Initialize state in the constructor or using the useState hook.
    • Avoid directly modifying state, instead use the setState function or the useState hook to update state.
    • Use functional updates when the new state depends on the previous state.
    • Avoid deeply nested state, consider using multiple state variables or a state management library like Redux for complex applications.
  • Props:

    • Pass props from parent components to child components to provide data.
    • Avoid modifying props directly in child components, as they are read-only.
    • Use props validation with PropTypes or TypeScript to ensure the correct types of props are passed.
    • Use default props to provide fallback values for optional props.

By following these best practices, you can write more maintainable and reusable React components.

5. How does React handle the re-rendering of components when state or props change?

When state or props change, React re-runs the component to produce a new tree of elements, then reconciles it against the previous tree:

  1. A state setter (useState/useReducer) or new props mark the component dirty.
  2. React re-renders it (and, by default, its descendants), building a new virtual tree.
  3. The reconciler diffs new vs old, using element type and key to match nodes.
  4. Only the minimal real DOM changes are committed; unchanged DOM is left alone.

Details that matter:

  • Updates are batched and rendering is concurrent (React 18+): React can interrupt/prioritise work (useTransition, useDeferredValue).
  • Re-rendering a component re-renders its children unless they're memoized (React.memo) or the work is skipped by the React Compiler (React 19), which auto-memoizes.
  • Re-render ≠ DOM update: React may re-run your function but commit nothing if the output is unchanged.
↑ Back to top

Follow-up 1

Can you provide an example of a component that re-renders when its state changes?

Sure! Here's an example of a simple React component that re-renders when its state changes:

import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <p>Count: {count}</p>
       setCount(count + 1)}&gt;Increment
    </div>
  );
}

Follow-up 2

What is the virtual DOM and how does it help in this process?

The virtual DOM is a lightweight copy of the actual DOM that React uses to perform efficient updates. When a component's state or props change, React first updates the virtual DOM instead of the actual DOM. It then compares the updated virtual DOM with the previous virtual DOM to determine the minimal set of changes needed to update the actual DOM. This approach helps in reducing the number of direct manipulations to the actual DOM, which can be expensive in terms of performance.

Follow-up 3

What are some performance considerations to keep in mind when working with state and props?

When working with state and props in React, there are a few performance considerations to keep in mind:

  1. Avoid unnecessary re-renders: Only update state or props when necessary to avoid triggering unnecessary re-renders. Use shouldComponentUpdate or React.memo to optimize component rendering.

  2. Use immutable data: Immutable data structures can help improve performance by allowing React to easily compare previous and current state or props.

  3. Batch updates: Use React's batch update mechanism, such as using setState with a function argument, to batch multiple state updates into a single re-render.

  4. Use key prop: When rendering lists of components, make sure to provide a unique key prop to each item. This helps React efficiently update and re-order the list when items are added, removed, or re-ordered.

By keeping these considerations in mind, you can optimize the performance of your React components.

Live mock interview

Mock interview: React State and Props

Intermediate ~5 min Your own free AI key

Your voice and your AI key never touch our servers; the key stays in this browser and is sent only to Google. Only your round scores are saved to track progress.