React

React interview questions

Prepare for your React interview with these common questions and answers.

01What is React?

React is a free and open-source front-end JavaScript library for building user interfaces based on UI components. It is maintained by Meta and a community of individual developers and companies.

02What are the major features of React?

The major features of React are:

  • JSX: A syntax extension to write HTML-like code in JavaScript.
  • Virtual DOM: A lightweight copy of the real DOM. React uses it to improve performance by minimizing direct manipulation of the DOM.
  • Component-Based Architecture: UI is broken down into reusable components.
  • Unidirectional Data Flow: Data flows in one direction, from parent to child components, making the application easier to debug.
03What is JSX?

JSX stands for JavaScript XML. It is a syntax extension for JavaScript that allows you to write HTML-like code in your React components. It's not a requirement for using React, but it's a helpful visual aid. JSX code must be compiled into regular JavaScript by a tool like Babel.

04What is the difference between a class component and a functional component?

Class Components: Are ES6 classes that extend React.Component. They have a render() method and can hold state and use lifecycle methods.

Functional Components: Are plain JavaScript functions that accept props and return React elements. With the introduction of Hooks, they can now also manage state and side effects.

05What are props in React?

Props (short for properties) are read-only attributes used to pass data from a parent component to a child component. They are immutable within the child component.

06What is state in a React component?

State is an object that represents the parts of the app that can change. Each component can maintain its own state. When a component's state changes, React re-renders the component.

07What are React Hooks? Name a few.

Hooks are functions that let you 'hook into' React state and lifecycle features from functional components. They were introduced in React 16.8.

Common Hooks include: useState, useEffect, useContext, useReducer, useCallback, useMemo.

08Explain the `useState` Hook.

useState is a Hook that lets you add React state to functional components. It returns a pair: the current state value and a function that lets you update it. Example: const [count, setCount] = useState(0);

09Explain the `useEffect` Hook.

useEffect is a Hook for performing side effects in functional components. Side effects can be data fetching, subscriptions, or manually changing the DOM. It runs after every render by default, but you can control when it runs by passing a dependency array.

10What is the Virtual DOM?

The Virtual DOM (VDOM) is a programming concept where a virtual representation of a UI is kept in memory and synced with the 'real' DOM. When a component's state changes, React creates a new virtual DOM tree, compares it with the previous one (this process is called 'diffing'), and then updates the real DOM with only the necessary changes.

11How does React handle events?

React events are named using camelCase, rather than lowercase. With JSX you pass a function as the event handler, rather than a string. React creates a 'synthetic event', which is a cross-browser wrapper around the browser's native event. This ensures events have consistent properties across different browsers.

12What is conditional rendering?

Conditional rendering in React works the same way conditions work in JavaScript. You can use if statements, ternary operators (condition ? true : false), or logical && operators to create elements representing the current state, and let React update the UI to match them.

13How do you handle lists in React? What is the importance of the `key` prop?

You can render lists of components by using the .map() function on an array. The key prop is a special string attribute you need to include when creating lists of elements. Keys help React identify which items have changed, are added, or are removed. They should be stable, predictable, and unique.

14What is prop drilling and how can you avoid it?

Prop drilling is the process of passing props down through multiple layers of nested components to reach a deeply nested child. It can be avoided using state management libraries like Redux or by using React's Context API.

15Explain the concept of lifting state up.

Lifting state up is a common pattern in React where you move the state from a child component to its closest common ancestor. This is done when multiple components need to share and reflect the same changing data.

16What is the Context API?

The Context API is a React structure that allows you to share state across the entire app (or part of it) lightly and with ease. It's designed to solve the problem of prop drilling. It consists of React.createContext, a Provider component, and a Consumer component or useContext Hook.

17What is the difference between controlled and uncontrolled components?

Controlled Components: An input form element whose value is controlled by React state. The value is set by state and updated via an onChange handler.

Uncontrolled Components: An input form element where the form data is handled by the DOM itself. You use a ref to get form values from the DOM.

18What are Higher-Order Components (HOCs)?

A Higher-Order Component is an advanced technique in React for reusing component logic. HOCs are not part of the React API, per se. They are a pattern that emerges from React’s compositional nature. A HOC is a function that takes a component and returns a new component with additional props or behavior.

19What is Redux?

Redux is a predictable state container for JavaScript apps. It helps you write applications that behave consistently, run in different environments (client, server, and native), and are easy to test. It provides a centralized store for state that needs to be used across your entire application.

20What is React Router?

React Router is a standard library for routing in React. It enables the navigation among views of various components in a React Application, allows changing the browser URL, and keeps the UI in sync with the URL.

21Explain the component lifecycle in class components.

The component lifecycle has three main phases:

  • Mounting: Putting elements into the DOM. (constructor, render, componentDidMount)
  • Updating: Updating the component with new data. (render, componentDidUpdate)
  • Unmounting: Removing the component from the DOM. (componentWillUnmount)
22What is code splitting in React?

Code-Splitting is a feature supported by bundlers like Webpack and Browserify which can create multiple bundles that can be dynamically loaded at runtime. React supports code-splitting with React.lazy. It helps in reducing the initial load time of the application.

23What are React Fragments?

Fragments let you group a list of children without adding extra nodes to the DOM. You can use <React.Fragment> or the shorter syntax <>...</>.

24What is the purpose of `useMemo` and `useCallback`?

They are performance optimization Hooks.

  • useMemo memoizes a value. It will only recompute the memoized value when one of the dependencies has changed.
  • useCallback memoizes a function. It will return a memoized version of the callback that only changes if one of the dependencies has changed.
25How do you optimize a React application's performance?

Several ways:

  • Using React.memo for components.
  • Using useMemo and useCallback Hooks.
  • Code-splitting using React.lazy.
  • Using production builds.
  • Windowing or list virtualization for long lists.
26What are synthetic events in React?

Synthetic events are React's cross-browser wrapper around the browser's native event system. They have the same interface as native events, including stopPropagation() and preventDefault(), but they work identically across all browsers.

27What is the difference between `props.children` and rendering a component?

props.children is a special prop that allows components to be passed as data to other components, just like any other prop. It's used for composition. Rendering a component directly (<MyComponent />) is the standard way to use a component, while props.children is used when a component needs to wrap or contain arbitrary child elements.

28What is an error boundary in React?

Error boundaries are React components that catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI instead of the component tree that crashed. They are implemented as class components with a componentDidCatch lifecycle method.

29What is Strict Mode in React?

<React.StrictMode> is a tool for highlighting potential problems in an application. Like Fragment, StrictMode does not render any visible UI. It activates additional checks and warnings for its descendants. It helps with identifying unsafe lifecycles, legacy API usage, and unexpected side effects.

30How would you make an API call in a React component?

You would typically use the useEffect Hook to make an API call when the component mounts. You can use the native fetch API or a library like axios. It's also important to handle loading and error states, often using useState.

Next step

Preparing for a role?

Tell us what you’re aiming for. A mentor can help you close the gaps before the interview.