Back to Interview Prep

React Interview Questions - React JS ReactJS Interview Prep

50 Questions Available

Comprehensive collection of 50 essential React.js interview questions covering hooks, components, state management, performance, and advanced concepts.

All Questions & Answers

Show per page:
1

What is React and what are its main features?

A

React is a JavaScript library for building user interfaces, particularly web applications. Its main features include component-based architecture, virtual DOM for efficient updates, one-way data binding, JSX syntax for writing HTML-like code in JavaScript, and a rich ecosystem of hooks and tools for state management and routing.

2

Explain the difference between functional and class components.

A

Functional components are JavaScript functions that return JSX. They are simpler, easier to test, and use hooks for state and lifecycle. Class components are ES6 classes that extend React.Component, use this.state and lifecycle methods. Functional components are now preferred in modern React development.

3

What is JSX and how does it differ from HTML?

A

JSX is a syntax extension for JavaScript that allows writing HTML-like code. It gets transpiled to React.createElement() calls. Differences include using className instead of class, camelCase for attributes like onClick, self-closing tags must have /, and JavaScript expressions are wrapped in curly braces.

4

What is the Virtual DOM and how does it work?

A

The Virtual DOM is a JavaScript representation of the real DOM. React creates a virtual representation, compares it with the previous version when state changes, identifies differences (diffing), and updates only the changed nodes in the real DOM (reconciliation). This makes updates faster than directly manipulating the DOM.

5

Explain React component lifecycle methods.

A

Lifecycle methods are hooks that allow you to run code at specific points in a component's lifecycle. In class components: componentDidMount (after first render), componentDidUpdate (after updates), componentWillUnmount (before removal). In functional components, useEffect hook replaces these methods.

6

What are props in React and how do they differ from state?

A

Props are read-only data passed from parent to child components. They are immutable and flow downward. State is internal data managed within a component, can be changed using setState or useState, and triggers re-renders when updated. Props are for configuration, state is for dynamic data.

7

How does useState hook work?

A

useState is a hook that adds state to functional components. It returns an array with two elements: the current state value and a function to update it. The initial value is set once on mount. Calling the update function triggers a re-render with the new state value.

8

What is the purpose of useEffect hook?

A

useEffect performs side effects in functional components. It runs after render and can handle data fetching, subscriptions, DOM manipulation, and cleanup. The dependency array controls when it runs: empty array runs once on mount, with dependencies runs when they change, no array runs after every render.

9

What is the difference between useEffect and useLayoutEffect?

A

useEffect runs asynchronously after the browser paints, while useLayoutEffect runs synchronously after DOM mutations but before the browser paints. Use useLayoutEffect when you need to read layout from DOM and synchronously re-render to prevent visual flashing. useEffect is preferred for most cases.

10

How do you handle events in React?

A

React uses synthetic events, a wrapper around native events for cross-browser compatibility. Event handlers are passed as props with camelCase names like onClick, onChange. The event object is passed automatically. Use arrow functions or bind to maintain this context in class components.

Showing 1 to 10 of 50 questions
...