Interview Questions

Get ready for your next interview with our comprehensive question library

React Interview Questions

Filter by Difficulty

1.

What is the Virtual DOM and how does it work?

beginner

The Virtual DOM is a JavaScript representation of the actual DOM (Document Object Model). It's a programming concept where a "virtual" representation of the UI is kept in memory and synced with the "real" DOM.

How it works:

  1. When state changes occur, React creates a new virtual DOM tree
  2. React compares (diffs) the new virtual DOM tree with the previous virtual DOM tree
  3. React calculates the minimum changes needed to update the real DOM
  4. React applies only these necessary changes to the real DOM

This process is called reconciliation and makes React applications faster because manipulating the virtual DOM is much faster than manipulating the real DOM.

2.

What is JSX and why is it used in React?

beginner

JSX (JavaScript XML) is a syntax extension for JavaScript that allows you to write HTML-like code within JavaScript. It makes React components more readable and easier to write.

JSX gets transpiled to React.createElement() calls by tools like Babel:

// JSX
const element = <h1>Hello, World!</h1>;

// Transpiled to
const element = React.createElement('h1', null, 'Hello, World!');

Benefits of JSX:

  • More intuitive and readable than React.createElement()
  • Allows mixing of HTML-like syntax with JavaScript expressions
  • Provides better error messages and warnings
  • Enables static type checking
3.

What's the difference between React elements and React components?

beginner

React Element: A plain JavaScript object that describes what should appear on screen. It's the smallest building block of React apps.

const element = <h1>Hello, World!</h1>;

React Component: A function or class that returns React elements. Components are reusable and can accept inputs (props).

// Function component
function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

// Class component
class Welcome extends React.Component {
  render() {
    return <h1>Hello, {this.props.name}</h1>;
  }
}
4.

What are the differences between functional and class components?

beginner

Functional Components:

  • Simpler syntax
  • Use React Hooks for state and lifecycle methods
  • Better performance (less overhead)
  • Easier to test and debug
  • Preferred approach in modern React
function MyComponent(props) {
  const [count, setCount] = useState(0);
  
  return <div>{count}</div>;
}

Class Components:

  • More verbose syntax
  • Use this.state and lifecycle methods
  • Have access to this context
  • Legacy approach (still supported)
class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = { count: 0 };
  }
  
  render() {
    return <div>{this.state.count}</div>;
  }
}
5.

How do you create a React component?

beginner

There are two main ways to create React components:

1. Function Component (Recommended):

function Welcome(props) {
  return <h1>Hello, {props.name}!</h1>;
}

// Or using arrow function
const Welcome = (props) => {
  return <h1>Hello, {props.name}!</h1>;
};

2. Class Component:

class Welcome extends React.Component {
  render() {
    return <h1>Hello, {this.props.name}!</h1>;
  }
}
6.

What are the rules of JSX?

beginner

Key JSX rules:

  1. Must return a single parent element or use React Fragment
  2. Use camelCase for attributes: className instead of class, onClick instead of onclick
  3. Close all tags: Self-closing tags must end with />
  4. Use curly braces for JavaScript expressions: {variable} or {expression}
  5. Boolean attributes: disabled={true} or just disabled
// Correct JSX
function MyComponent() {
  return (
    <div className="container">
      <img src="image.jpg" alt="Description" />
      <p>Count: {count}</p>
    </div>
  );
}
7.

What is React Fragment and when would you use it?

beginner

React Fragment lets you group multiple elements without adding an extra node to the DOM. It's useful when you need to return multiple elements from a component but don't want to wrap them in a div.

// Using React.Fragment
function MyComponent() {
  return (
    <React.Fragment>
      <h1>Title</h1>
      <p>Description</p>
    </React.Fragment>
  );
}

// Using short syntax
function MyComponent() {
  return (
    <>
      <h1>Title</h1>
      <p>Description</p>
    </>
  );
}
8.

How do you conditionally render elements in React?

beginner

There are several ways to conditionally render elements:

1. Ternary Operator:

function Greeting({ isLoggedIn }) {
  return (
    <div>
      {isLoggedIn ? <h1>Welcome back!</h1> : <h1>Please log in.</h1>}
    </div>
  );
}

2. Logical AND (&&):

function Notification({ hasMessages }) {
  return (
    <div>
      {hasMessages && <p>You have new messages!</p>}
    </div>
  );
}

3. If-else statements:

function UserStatus({ user }) {
  if (user.isAdmin) {
    return <AdminPanel />;
  } else if (user.isLoggedIn) {
    return <UserPanel />;
  } else {
    return <LoginForm />;
  }
}
9.

How do you render lists in React?

beginner

Use the map() method to render lists, and always provide a unique key prop:

function TodoList({ todos }) {
  return (
    <ul>
      {todos.map(todo => (
        <li key={todo.id}>
          {todo.text}
        </li>
      ))}
    </ul>
  );
}

The key prop helps React identify which items have changed, been added, or removed, improving performance during re-renders.

10.

What are props in React?

beginner

Props (short for properties) are read-only inputs passed from parent components to child components. They allow data to flow down the component tree.

// Parent component
function App() {
  return <Welcome name="John" age={25} />;
}

// Child component
function Welcome(props) {
  return <h1>Hello, {props.name}! You are {props.age} years old.</h1>;
}

Props characteristics:

  • Read-only (immutable)
  • Passed from parent to child
  • Can be any JavaScript value (strings, numbers, objects, functions, etc.)
11.

What is state in React?

beginner

State is a built-in React object used to contain data that may change over the lifetime of a component. When state changes, the component re-renders.

import { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);
  
  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>
        Increment
      </button>
    </div>
  );
}

State characteristics:

  • Mutable (can be changed)
  • Local to the component
  • Triggers re-renders when updated
  • Should not be modified directly
12.

What's the difference between props and state?

beginner
Props State
Read-only Mutable
Passed from parent Local to component
Cannot be changed by component Can be changed by component
External data Internal data
Functional parameters Component memory
13.

How do you handle events in React?

beginner

React uses SyntheticEvents, which are wrappers around native events that provide consistent behavior across browsers:

function Button() {
  const handleClick = (event) => {
    event.preventDefault();
    console.log('Button clicked!');
    console.log('Event type:', event.type);
  };
  
  return (
    <button onClick={handleClick}>
      Click me
    </button>
  );
}
14.

How do you prevent default behavior in React events?

beginner

Use the preventDefault() method on the event object:

function Form() {
  const handleSubmit = (event) => {
    event.preventDefault(); // Prevents form from submitting
    console.log('Form submitted');
  };
  
  return (
    <form onSubmit={handleSubmit}>
      <input type="text" />
      <button type="submit">Submit</button>
    </form>
  );
}
15.

How do you pass parameters to event handlers?

beginner

There are several ways to pass parameters:

1. Arrow function in JSX:

function ItemList({ items }) {
  const handleClick = (id) => {
    console.log('Clicked item:', id);
  };
  
  return (
    <div>
      {items.map(item => (
        <button key={item.id} onClick={() => handleClick(item.id)}>
          {item.name}
        </button>
      ))}
    </div>
  );
}

2. Bind method:

function ItemList({ items }) {
  const handleClick = (id, event) => {
    console.log('Clicked item:', id);
  };
  
  return (
    <div>
      {items.map(item => (
        <button key={item.id} onClick={handleClick.bind(null, item.id)}>
          {item.name}
        </button>
      ))}
    </div>
  );
}
16.

What is useState hook and how do you use it?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
17.

What are controlled vs uncontrolled components?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
18.

What is Create React App and what does it provide?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
19.

What is the difference between React and ReactDOM?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
20.

How do you pass data from child to parent component?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
Showing 1 to 20 of 103 results

Premium Plan

$10.00 /monthly
  • Access all premium content - interview questions, and other learning resources

  • We regularly update our features and content, to ensure you get the most relevant and updated premium content.

  • 1000 monthly credits

  • Cancel anytime