Skip to main content

React Interview Guide: 35 Must-Know Questions & Answers

 35 React Interview Questions and Answers



Introduction

React is one of the most popular JavaScript libraries for building user interfaces. Whether you're preparing for a job interview or just want to deepen your understanding, this guide covers 100 commonly asked React interview questions with answers.


1. Basic React Questions

1. What is React?

React is an open-source JavaScript library developed by Facebook for building fast and interactive user interfaces, particularly single-page applications.

2. What are the main features of React?

  • JSX (JavaScript XML)
  • Virtual DOM
  • One-way data binding
  • Component-based architecture
  • High performance

3. What are components in React?

Components are independent, reusable pieces of UI in React. They can be functional or class-based.

4. What is JSX?

JSX is a syntax extension of JavaScript that allows you to write HTML inside JavaScript.

jsx
const element = <h1>Hello, world!</h1>;

5. What is the Virtual DOM?

The Virtual DOM is a lightweight copy of the real DOM that React uses to optimize updates and re-rendering.


2. React Components and Props

6. What is the difference between functional and class components?

  • Functional components: Simple, stateless functions.
  • Class components: More complex, can hold state and lifecycle methods.

7. What are props in React?

Props (short for properties) are used to pass data between components.

8. How do you pass props in React?

jsx
function Greeting(props) { return <h1>Hello, {props.name}!</h1>; } <Greeting name="John" />;

9. Can props be changed inside a component?

No, props are immutable. They cannot be modified inside the component.

10. What is prop drilling?

Prop drilling is passing data through multiple components when it's needed deep in the component tree.


3. React State and Lifecycle

11. What is state in React?

State is an object that holds component-specific data and can change over time.

12. How do you define state in a class component?

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

13. How do you update state?

Use setState():

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

14. What is the difference between state and props?

  • State: Local to the component and mutable.
  • Props: Passed from parent and immutable.

15. What are React lifecycle methods?

Lifecycle methods control the behavior of components at different stages.


4. Hooks in React

16. What are React Hooks?

Hooks allow functional components to use state and lifecycle features.

17. What is the useState hook?

It allows adding state to functional components.

jsx
const [count, setCount] = useState(0);

18. What is the useEffect hook?

useEffect is used for side effects like fetching data.

jsx
useEffect(() => { console.log("Component mounted!"); }, []);

19. What is the useContext hook?

It allows components to access context values without prop drilling.

20. What is the useRef hook?

It creates a reference to a DOM element or component instance.


5. Handling Events and Forms

21. How do you handle events in React?

jsx
<button onClick={handleClick}>Click me</button>

22. How do you pass parameters in event handlers?

jsx
<button onClick={() => handleClick(id)}>Click me</button>

23. How do you handle forms in React?

jsx
const [input, setInput] = useState(""); const handleChange = (e) => setInput(e.target.value);

6. React Router

24. What is React Router?

React Router is a library for handling navigation in React apps.

25. How do you use React Router?

jsx
<BrowserRouter> <Route path="/" component={Home} /> </BrowserRouter>

7. Redux in React

26. What is Redux?

Redux is a state management library for React applications.

27. What are the core principles of Redux?

  • Single source of truth
  • State is read-only
  • Changes are made with pure functions

8. Performance Optimization

28. What is React.memo?

It prevents unnecessary re-renders by memoizing components.

29. What is lazy loading in React?

Lazy loading delays component loading until needed.


9. Advanced React

30. What are higher-order components (HOCs)?

HOCs are functions that take a component and return a new component.

31. What is Context API?

It provides a way to pass data through the component tree without prop drilling.

jsx
const MyContext = React.createContext();

32. What is reconciliation in React?

Reconciliation is the process React uses to update the DOM efficiently.


10. Miscellaneous

33. What are controlled components?

Controlled components have their state controlled by React.

34. What are uncontrolled components?

Uncontrolled components manage their own state using refs.

35. What is the difference between React and React Native?

  • React: For building web applications.
  • React Native: For building mobile applications.

Conclusion

These are just some of the most commonly asked questions in React interviews. Mastering these concepts will help you confidently tackle interviews and build better React applications. Keep practicing, building projects, and staying updated with the latest React features!