ReactJs Interview Questions
ReactJs Interview Questions
js
Questions
with Answers to Crack the
Technical Interview
*Disclaimer*
Everyone learns uniquely.
www.bosscoderacademy.com 1
Easy
Q.1
What are the features of React?
Ans ->
The features of React are as follows
JSX
Components
Virtual DOM
www.bosscoderacademy.com 2
Easy
Data Binding
www.bosscoderacademy.com 3
Easy
Q.2
What is difference between
Element
www.bosscoderacademy.com 4
Easy
type: 'div',
props: {
children: 'Login',
id: 'login-btn'
www.bosscoderacademy.com 5
Easy
Component
Login
</div>
);
React.createElement(
"div",
www.bosscoderacademy.com 6
Easy
"Login"
);
www.bosscoderacademy.com 7
Easy
Q.3
How to create components in React?
Ans ->
Function Components
Class Components
www.bosscoderacademy.com 8
Easy
JSX
render() {
www.bosscoderacademy.com 9
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?
Faster Updates
www.bosscoderacademy.com 11
Easy
Efficient Updating
www.bosscoderacademy.com 12
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
const books = [
];
www.bosscoderacademy.com 13
Easy
Note
It's crucial to use unique keys among siblings to avoid
issues.
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.
If you extract list items into separate components, apply
keys to the component instead of the li tag.
The "key" attribute accepts either a string or a number,
and it's converted internally to a string type.
A warning message will appear in the console if the
"key" prop is not present on list items.
www.bosscoderacademy.com 14
Easy
Q.6
Explain the steps to create a react
application and print hello world?
Ans ->
Steps to Create a React Applicatio
Install Node
www.bosscoderacademy.com 15
Easy
JSX
cd my-react-app
function App() {
return (
<div>
<h1>Hello World!</h1>
</div>
);
www.bosscoderacademy.com 16
Easy
npm start
www.bosscoderacademy.com 17
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 18
Easy
*/}
</div>
www.bosscoderacademy.com 19
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
});
ReactDOM.render(
<ul>
{fruitList}
www.bosscoderacademy.com 20
Easy
www.bosscoderacademy.com 21
Easy
Q.9
Explain the difference between
functional components and class
components.
Ans ->
Functional Component
Definition
Rendering
State
Lifecycle Methods
www.bosscoderacademy.com 22
Easy
Constructor
Class Component
Definition
Rendering
State
Lifecycle Methods
Constructor
www.bosscoderacademy.com 23
Easy
Summary
Functional components are simple functions that accept
props and return JSX. They are stateless and don't use a
constructor or React lifecycle methods.
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 24
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 25
Easy
return (
<div>
<p>Count: {count}</p>
</div>
);
www.bosscoderacademy.com 26
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 27
Easy
</div>
);
www.bosscoderacademy.com 28
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
www.bosscoderacademy.com 30
Medium
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 31
Medium
JSX
function ExampleApplication() {
return (
<div>
<Header />
<React.StrictMode>
<div>
<ComponentOne />
<ComponentTwo />
</div>
</React.StrictMode>
<Header />
</div>
);
www.bosscoderacademy.com 32
Medium
Q.14
How is React different from
Angular?
Ans ->
www.bosscoderacademy.com 33
Medium
Q.15
What are the different phases of
www.bosscoderacademy.com 34
Medium
Updating
www.bosscoderacademy.com 35
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 36
Medium
getDerivedStateFromProps(
Called just before rendering elements in the DOM.
Sets up the state based on initial props.
First method called on component update
render(
Outputs or re-renders HTML to the DOM with new
changes.
Essential method called on every render
componentDidMount(
Called after component rendering.
Executes statements requiring the component to be in
the DOM
shouldComponentUpdate(
Returns a Boolean specifying whether React should
proceed with rendering.
Default value is true
getSnapshotBeforeUpdate(
Provides access to props and state before the update.
Allows checking previous values after the update.
www.bosscoderacademy.com 37
Medium
componentDidUpdate(
Called after the component is updated in the DOM
componentWillUnmount(
Called when the component is about to be removed
from the DOM.
www.bosscoderacademy.com 38
Medium
Q.17
What is prop drilling?
Ans ->
The lifecycle of a React component is divided into four
phases:
Example Scenari
Consider a scenario where <EditUsersPage />
maintains selectedUserAddress in its state.
<EditUsersPage /> renders <User />, which, in turn,
renders <UserDetails />.
<UserDetails /> contains a <UserAddress />
component that requires access to
selectedUserAddress.
Approach
www.bosscoderacademy.com 39
Medium
www.bosscoderacademy.com 40
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 41
Medium
Rout
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
Similar to an anchor tag in HTML, this helps create links
to different routes, making navigation smooth across
your application.
www.bosscoderacademy.com 42
hard
Q.19
What are Custom Hooks in React?
Ans ->
Custom Hooks in React
Purpose
Exampl
Consider a custom hook for handling form input:
www.bosscoderacademy.com 43
hard
JSX
// useInput.js
setValue(e.target.value);
};
return {
value,
onChange: handleChange,
};
};
Usage in a Componen
Now, you can use the useInput custom hook in any
component to manage input state:
www.bosscoderacademy.com 44
hard
JSX
return (
<form>
<label>Username:
</label>
<label>Password:
</label>
</form>
);
};
www.bosscoderacademy.com 45
hard
Explanatio
The useInput hook abstracts away the state
management and event handling for input fields.
The component using this custom hook can easily
manage multiple input fields without duplicating similar
logic.
www.bosscoderacademy.com 46
hard
Q.20
What are higher order components
in React?
Ans ->
Definitio
HOCs in React are functions that take a component and
return an enhanced version, leveraging React's
compositional nature.
Purity of HOCs
Usage Pattern
const EnhancedComponent =
higherOrderComponent(WrappedComponent);
www.bosscoderacademy.com 47
hard
Use Case
Code Reuse and Logic Abstraction
-> Encapsulate and reuse code, abstracting logic for
enhanced components
Render Hijacking
-> Customize component rendering by intercepting and
modifying the process
State and Props Manipulation
-> Manage state within HOCs, manipulate or enhance
props before passing them down.
Advantage
Modularity and Separation of Concerns
-> Enhances code organization by separating concerns
like state, logic, and rendering
Composability
-> Compose multiple HOCs for granular and reusable
component composition
Encapsulation
-> Encapsulates specific functionalities, improving code
clarity and testability.
www.bosscoderacademy.com 48
Why
Bosscoder?
1000+ Alumni placed at Top
Product-based companies.
Explore More