1 React Interview Questions Web Dev Mastery
1 React Interview Questions Web Dev Mastery
Basic Level:
1. What is React?
2. What is JSX?
3. What are Components in React?
4. What is the difference between Functional and Class Components?
5. What is the Virtual DOM?
6. What are Props?
7. What Is the State in React?
8. What is the difference between State and Props?
9. What is the use of the useState hook?
10. What is the use of the useEffect hook?
Intermediate Level:
1. What is the purpose of Keys in React?
2. What are React Fragments?
3. What is the Context API in React?
4. What is Higher-Order Component (HOC)?
5. What are React Hooks?
6. What is useMemo and when would you use it?
7. What is the use of useCallback?
8. What are controlled and uncontrolled components in React?
9. What are Pure Components?
10. What is Prop Drilling and how can you avoid it?
Advanced Level:
1. What is Redux and how is it used in React?
2. What are React Portals?
3. What is React Suspense?
4. How does React handle performance optimization?
5. What is Server-Side Rendering (SSR) in React?
6. What is the difference between useReducer and useState?
7. What is React Fiber?
8. What is Code Splitting and how is it implemented in React?
9. What is Reconciliation in React?
10. How does React.memo work?
11. What is Lazy Loading in React?
12. How do you handle form validation in React?
13. What is Strict Mode in React?
14. What are the Life Cycle Methods In React?
Basic Level:
1. What is React?
○ React is a JavaScript library used for building user interfaces,
primarily for single-page applications. It’s maintained by Facebook
and focuses on component-based architecture.
2. What is JSX?
○ JSX stands for JavaScript XML. It is a syntax extension for
JavaScript that looks similar to HTML and is used to describe what
the UI should look like in React components.
1. Initial Rendering: When a React application is rendered for the first time,
a virtual representation of the DOM is created. This is an exact copy of the
actual DOM.
2. State/Prop Changes: When there is a change in state or props, React
creates a new version of the virtual DOM.
3. Reconciliation: React compares the new virtual DOM with the previous
one to determine what has changed. This process is called Diffing.
4. Minimal DOM Updates: After identifying the changes, React updates only
the specific parts of the actual DOM that have changed, rather than
re-rendering the entire DOM. This makes the process faster and more
efficient.
Example:
1. Initial Render: When the app is first rendered, React creates a virtual
DOM representing the structure of the counter.
2. Click Event: When the user clicks the button, the count state is
updated to 1.
React creates a new virtual DOM with the updated count value (1).
3. Diffing: React compares the old virtual DOM (where the count was
0) with the new one (where the count is 1).
It identifies that only the text content inside the <h1> tag has
changed.
Project Structure:
1. withLogger.jsx (HOC)
This file contains the Higher Order Component that enhances any component it
wraps by logging props.
2. Hello.js
This is the main entry point where we apply the HOC to the Hello component
and render it.
Advanced Level:
1. What is Redux and how is it used in React?
○ Redux is a state management library that allows you to manage the
global state of your application. It follows a unidirectional data flow
and is often used with React to manage complex states.
2. What are React Portals?
○ React Portals provide a way to render components outside the
normal React DOM hierarchy, i.e., render child components into a
DOM node that exists outside of the parent component’s DOM
hierarchy.
○ In typical React components, everything is rendered inside the same
root element. But sometimes, you need to render components in a
different part of the DOM, like modals, tooltips, or overlays, which
should appear on top of everything else and outside the usual
component structure. React Portals make this possible.
HTML (index.html):
React (App.js):
3. What is React Suspense?
○ React Suspense allows you to delay rendering components until
some asynchronous data is loaded, improving the user experience
by handling loading states more efficiently.
4. How does React handle performance optimization?
○ React optimizes performance using techniques like memoization
(React.memo), using keys in lists, lazy loading, code splitting,
useMemo, useCallback, and avoiding unnecessary re-renders by
using Pure Components.
Home.js:
About.js:
2. Implement Code Splitting in Your Main Component
9. What is Reconciliation in React?
○ Reconciliation is the process React uses to determine what changes
need to be made to the DOM based on differences between the
Virtual DOM and the real DOM.
How it Works:
1. Virtual DOM: React creates a lightweight copy of the actual web page
(called the virtual DOM).
2. Changes Detection: When you update something (like clicking a button to
change text), React doesn't update the whole web page. Instead, it first
updates the virtual DOM to see what has changed.
3. Finding Differences: React compares the new virtual DOM with the
previous one and finds what has changed (for example, if just one button
or a text changed).
4. Updating the Actual DOM: Based on the differences it found, React only
updates the specific parts of the actual webpage (real DOM) that changed,
instead of reloading the entire page.
Example:
If you have a list of items and you add a new one, React will:
Lifecycle methods are hooks in React that allow developers to run code at
specific points in a component's life cycle, such as when the component is
created, updated, or removed. They are primarily used in class
components and are crucial for managing side effects, fetching data, and
handling component behavior throughout its lifespan.
1. constructor(props):
○ Called before the component is mounted.
○ Used to initialize state and bind methods.
2. componentDidMount():
○ Invoked immediately after a component is mounted.
○ Commonly used for data fetching, subscriptions, or setting up
timers.
3. shouldComponentUpdate(nextProps, nextState):
○ Determines whether the component should re-render based on
changes in props or state.
○ Returns true or false.
4. componentDidUpdate(prevProps, prevState):
○ Called immediately after updating occurs.
○ Can be used to perform operations after a component has
updated (e.g., fetching data based on prop changes).
5. componentWillUnmount():
○ Invoked immediately before a component is unmounted and
destroyed.
○ Ideal for cleanup tasks, like canceling network requests or
removing event listeners.
6. componentDidCatch(error, info):
○ Used for error handling in child components.
○ Allows you to log errors and display fallback UI.