Most Asked React Interview Questions ?
Most Asked React Interview Questions ?
js
Questions
with Answers to Crack the
Technical Interview
*Disclaimer*
Everyone learns uniquely.
Q.1
What are the features of React?
Ans ->
The features of React are as followsQ
5Z JSX
www.bosscoderacademy.com 1
Easy
BOSSCODER
ACADEMY
www.bosscoderacademy.com 2
Easy
Q.2
What is difference between
Element
www.bosscoderacademy.com 3
Easy
type: 'div',
props: {
children: 'Login',
id: 'login-btn'
www.bosscoderacademy.com 4
Easy
Component
Login
</div>
);
React.createElement(
"div",
www.bosscoderacademy.com 5
Easy
"Login"
);
BOSSCODER
ACADEMY
www.bosscoderacademy.com 6
Easy
Q.3
How to create components in React?
Ans ->
6G Function Components
JSX
class Greeting extends React.Component {
render() {
www.bosscoderacademy.com 8
Easy
Q.4
What is the Virtual DOM?
Ans ->
The Virtual DOM is like a blueprint or a copy of the real
DOM that is stored in the computer's memory. It's a
concept used by React to make updating and changing
things on a webpage more efficient.
Why is it Needed?
www.bosscoderacademy.com 10
Easy
www.bosscoderacademy.com 11
Easy
Q.5
What are keys in React and why do
we need them?
Ans ->
The "key" is a special attribute used when working with
arrays of elements in React. It helps React keep track of
changes, additions, and removals in the array.
Example
];
www.bosscoderacademy.com 12
Easy
Note
u It's crucial to use unique keys among siblings to avoid
issues.}
u If your data doesn't have stable IDs, using the item index
as a key is a last resort. However, this is not
recommended if the order of items may change, as it
can impact performance.}
u If you extract list items into separate components, apply
keys to the component instead of the li tag.}
u The "key" attribute accepts either a string or a number,
and it's converted internally to a string type.}
u A warning message will appear in the console if the
"key" prop is not present on list items.
www.bosscoderacademy.com 13
Easy
Q.6
Explain the steps to create a react
application and print hello world?
Ans ->
Steps to Create a React Applicatio_
]p Install Node
www.bosscoderacademy.com 14
Easy
JSX
cd my-react-app
function App() {
return (
<div>
<h1>Hello World!</h1>
</div>
);
www.bosscoderacademy.com 15
Easy
www.bosscoderacademy.com 16
Easy
Q.7
How are comments written in React?
Ans ->
Comments in React/JSX are similar to JavaScript multiline
comments but are enclosed in curly braces.
Single-line comments
JSX
<div>
</div>
Multi-line comments
JSX
<div>
{/*
www.bosscoderacademy.com 17
Easy
*/}
</div>
BOSSCODER
ACADEMY
www.bosscoderacademy.com 18
Easy
Q.8
Explain how lists are created
in React?
Ans ->
Lists are essential for displaying dynamic content on a
website. In React, you can create a list using the map
method of an array. Here's an example:
JSX
import React from 'react';
});
ReactDOM.render(
<ul>
{fruitList}
www.bosscoderacademy.com 19
Easy
BOSSCODER
ACADEMY
www.bosscoderacademy.com 20
Easy
Q.9
Explain the difference between
functional components and class
components.
Ans ->
Functional Component|
js Definition
Xf Constructor
Class Component8
5f Definition
www.bosscoderacademy.com 22
Easy
Summary
3 Functional components are simple functions that accept
props and return JSX. They are stateless and don't use a
constructor or React lifecycle methods.:
3 Class components are ES6 classes that extend
React.Component. They have a render method, can use
state, a constructor, and React lifecycle methods. They
are suitable for managing state and implementing more
complex logic.
www.bosscoderacademy.com 23
Easy
Q.10
What are React Hooks?
Ans ->
React Hooks are built-in functions introduced in React
version 16.8 that allow developers to utilize state and
lifecycle methods within functional components. They
enhance code reusability and provide flexibility in
navigating the component tree.
function Counter() {
www.bosscoderacademy.com 24
Easy
return (
<div>
<p>Count: {count}</p>
</div>
);
www.bosscoderacademy.com 25
Easy
Q.11
What is useState() in React?
Ans ->
The useState() is a fundamental React Hook used to
introduce state variables into functional components,
especially when dynamic control over elements in the DOM
is required.
function Greeting() {
return (
<div>
<p>{message}, React!</p>
www.bosscoderacademy.com 26
Easy
</div>
);
www.bosscoderacademy.com 27
Medium
Q.12
What are the different types of
Hooks in React?
Ans ->
Basic Hooks
useState()
useEffect()
useContext()
Additional Hooks
useReducer()
useCallback()
useImperativeHandle()
useDebugValue()
useRef()
useLayoutEffect()
Custom Hooks
Q.13
What is Strict Mode in React?
Ans ->
React.StrictMode is a component designed to highlight
potential issues and enforce best practices in a React
application. It does not introduce additional DOM elements
and operates exclusively in development mode.
Usage
Example
www.bosscoderacademy.com 30
Medium
JSX
import React from "react";
function ExampleApplication() {
return (
<div>
<Header />
<React.StrictMode>
<div>
<ComponentOne />
<ComponentTwo />
</div>
</React.StrictMode>
<Header />
</div>
);
www.bosscoderacademy.com 31
Medium
Q.14
How is React different from
Angular?
Ans ->
www.bosscoderacademy.com 32
Medium
Q.15
What are the different phases of
www.bosscoderacademy.com 33
Medium
?1 Updating
www.bosscoderacademy.com 34
Medium
Q.16
What are the lifecycle methods of
React?
Ans ->
React lifecycle methods are functions automatically called
at different phases in a component's lifecycle, offering
control over its behavior. Understanding and utilizing these
methods empower developers to efficiently manage various
aspects throughout the component's existence.
Example Scenario
www.bosscoderacademy.com 35
Medium
PY getDerivedStateFromProps(H
E Called just before rendering elements in the DOM.;
E Sets up the state based on initial props.;
E First method called on component update4
2Y render(H
E Outputs or re-renders HTML to the DOM with new
changes.;
E Essential method called on every render4
Y componentDidMount(H
E Called after component rendering.;
E Executes statements requiring the component to be in
the DOM4
Y shouldComponentUpdate(H
E Returns a Boolean specifying whether React should
proceed with rendering.;
E Default value is true4
>Y getSnapshotBeforeUpdate(H
E Provides access to props and state before the update.;
E Allows checking previous values after the update.
www.bosscoderacademy.com 36
Medium
2= componentDidUpdate(,
( Called after the component is updated in the DOM
1= componentWillUnmount(,
( Called when the component is about to be removed
from the DOM.
BOSSCODER
ACADEMY
www.bosscoderacademy.com 37
Medium
Q.17
What is prop drilling?
Ans ->
The lifecycle of a React component is divided into four
phases:
Example Scenari8
W Consider a scenario where <EditUsersPage />
maintains selectedUserAddress in its state.S
W <EditUsersPage /> renders <User />, which, in turn,
renders <UserDetails />.S
W <UserDetails /> contains a <UserAddress />
component that requires access to
selectedUserAddress.
Approach
www.bosscoderacademy.com 38
Medium
www.bosscoderacademy.com 39
Medium
Q.18
What is React Router?
Ans ->
React Router is like a navigation manager for React
applications. It helps build single-page web apps where you
can navigate to different sections without refreshing the
entire page. This keeps the user experience smooth and
also updates the browser URL as you move around.
www.bosscoderacademy.com 40
Medium
2: RoutB
F This is where the action happens. Whenever the URL
matches the path you set, this component decides
what UI to show. It's like a conditionally displayed part
of your app(
: Lin@
F Similar to an anchor tag in HTML, this helps create links
to different routes, making navigation smooth across
your application.
www.bosscoderacademy.com 41
hard
Q.19
What are Custom Hooks in React?
Ans ->
Custom Hooks in React
Purpose
ExamplR
. Consider a custom hook for handling form input:
www.bosscoderacademy.com 42
hard
JSX
// useInput.js
setValue(e.target.value);
};
return {
value,
onChange: handleChange,
};
};
Usage in a ComponenR
Q Now, you can use the useInput custom hook in any
component to manage input state:
www.bosscoderacademy.com 43
hard
JSX
import React from 'react';
return (
<form>
<label>Username:
</label>
<label>Password:
</label>
</form>
);
};
www.bosscoderacademy.com 44
hard
ExplanatioO
8 The useInput hook abstracts away the state
management and event handling for input fields.*
8 The component using this custom hook can easily
manage multiple input fields without duplicating similar
logic.
www.bosscoderacademy.com 45
hard
Q.20
What are higher order components
in React?
Ans ->
Definitio^
R HOCs in React are functions that take a component and
return an enhanced version, leveraging React's
compositional nature.
Purity of HOCs
Usage Pattern
www.bosscoderacademy.com 46
hard
Use CaseX
? Code Reuse and Logic Abstraction
-> Encapsulate and reuse code, abstracting logic for
enhanced componentsD
? Render Hijacking
-> Customize component rendering by intercepting and
modifying the processD
? State and Props Manipulation
-> Manage state within HOCs, manipulate or enhance
props before passing them down.
AdvantageX
? Modularity and Separation of Concerns
-> Enhances code organization by separating concerns
like state, logic, and renderingD
? Composability
-> Compose multiple HOCs for granular and reusable
component compositionD
? Encapsulation
-> Encapsulates specific functionalities, improving code
clarity and testability.
www.bosscoderacademy.com 47
Why
Bosscoder?
1000+ Alumni placed at Top
Product-based companies.
Explore More