ReactJS Interview Questions
ReactJS Interview Questions
Ans A library is a collection of pre-written code (functions, classes, utilities, modules, etc.) that
developers can use to perform common tasks without writing everything from scratch.
Ans An SPA is a web app that loads only one HTML page (usually [Link]) and updates the content
dynamically using JavaScript instead of reloading the entire page for whole navigation.
Que Why use ReactJS instead of other libraries like (AngularJS, VueJS)?
Ans Because
● It uses Virtual DOM
● Easy to learn that other technologies
● High demand and lots of jobs in Market
● Large Community
● After Learning ReactJS we can easily learn React Native for mobile development
Ans The Virtual DOM (VDOM) is a lightweight, in-memory representation of the actual DOM
(Document Object Model).
How it Works:
● React creates a virtual copy of the UI.
● When the state of a component changes, React first updates this Virtual DOM.
● It then compares the updated Virtual DOM with the previous Virtual DOM to identify the
differences (diffing algorithm).
● Only the parts of the actual DOM that have changed are then updated, rather than
re-rendering the entire UI. This process is called Reconciliation.
Benefits:
● Performance: Minimizes direct manipulation of the actual DOM, which is slow.
● Efficiency: Batches multiple updates to the real DOM, reducing rendering time.
● When you write JSX, React compiles it into React elements (plain JS objects).
● These objects describe what the UI should look like.
● React builds a tree of these objects = Virtual DOM tree.
Beyond a simple Virtual DOM, modern React uses Fiber. This is an advanced re-implementation of
the reconciliation algorithm, allowing React to:
This architecture significantly improves performance for animations and complex applications.
Ans Think of Vite like a super-fast kitchen for cooking your web app.
Vite is a super-fast tool that helps developers run and build modern web apps more efficiently
than older tools like Webpack
👉 It helps developers install, share, and manage packages (reusable pieces of code) for their
JavaScript projects.
● Package Installation: Allows you to install third-party libraries and tools into your project.
● Dependency Management: Tracks all the packages your project needs in a [Link]
file.
● Script Runner: Can execute scripts defined in your [Link] (e.g., npm start, npm
test).
● Publishing Packages: Enables developers to publish their own packages for others to use.
Ans A CSS Preprocessor is a tool that lets you write CSS in a more powerful, advanced language
(with features normal CSS doesn’t have), and then it compiles (transpiles) it into plain CSS that
browsers understand.
Ans A bundler is a tool that takes all your project’s files (JavaScript, CSS, images, etc.) and combines
(bundles) them into fewer files that the browser can understand and load efficiently.
Vite is a frontend build tool that uses a dev server + a bundler under the hood.
Que Why do we need bundlers?
Ans
Modern web apps are built with:
Iske ander ek [Link] hai which looks like this initially looks like the file below image
Ye kya kar rha hai ki ye getElementById(‘root’) se uper wale [Link] de root class wale div ko
dhoondha aur jitna bhi app wala code hai </App> usko is div ke ander dal diya.
[Link] Output
Que Why do we need to start a component’s Name in React With Capital Letter?
Ans Capitalizing React component names is a crucial convention for several reasons:
1. Distinguishes from HTML: React uses capitalization to differentiate your custom
components (e.g., MyComponent) from standard HTML tags (e.g., div). If lowercase, React
would treat it as an unknown HTML element.
2. JSX Transpilation: This convention is vital for how JSX is converted into
[Link]() calls. Capitalized tags become component references, while
lowercase tags become string names for HTML elements.
3. Readability: Uppercase starting letters make it easier to quickly identify custom components
in your codebase, improving readability and maintainability.
4. Avoids Conflicts: It prevents potential naming clashes with current and future lowercase
HTML elements.
In essence, capitalizing component names ensures correct React interpretation, proper JSX
handling, and enhances code clarity.
Ans Here it is
Folders:
Files:
Ans Jab ham koi badi cheej banate hai to uske chote chote parts ko component kehte hai. To usi tarah ek
react project ka ek chota sa part hota hai component.
The image shown below shows a React App with multiple compnents
Ek component ke ander further aur components bana sakte hai. For Ex: SIDENAV ke ander ek aur
component bana sakte hai
Ans Ya to tum [Link] me multiple components banao ya alag se bana ke [Link] me import karke use
Karo. Ham dusra bana ke imprt krne wala method dekhte hai pehle tum khud karna
First make a component and export it and after that,
You can use it by importing it in a file in a self-closing or non self-closing tag <component/>
Que How would you differentiate between a React component and a JavaScript Function?
Ans Uper ke images se dekho component me JSX syntax hoga aur function me keval JavaScript ka code
hoga.
Ans Jab ek component bana ke dusre file me bhejte ho to uso bolte hai export aur jis file me use krte ho
us component ko manga ke usko bolte hai import “How to use a React component" wale question
me dekho.
Default Export:
Ek file (here UserComponents) se ek hi default export allowed hota hai baki agar aur exports karne
hai same file se to named export kar sakte ho
[Link] [Link]
Named Export:
Agar named export karte ho to curly braces laga ke import karna padta hai
[Link] [Link]
Ans JSX is a syntax extension for JavaScript that lets you write HTML like markup inside an JavaScript
file. Although there are other ways to write components without JSX.
Some people call JSX as JavaScript Extension and some call it JavaScript XML but in the official
documentation nothing is mentioned.
Ans For this we have to Use createElement and then create an element with it and then return that
element. It is a bit inconvenient to use this method rather we should use JSX
Que JSX With Curly Braces
Ans onClick in ReactJS Expects the definition of a function not its calling
See the examples below to understand
If u do like this for calling then the alert will This is the correct way because here we are
come before calling of the function passing the definition of an arrow function
Que Which fruitName function will be called here as shown in the image code below?
Ans Ander wala call hoga because JS (and by extension React) first looks for it in the current scope first
Que Follow Up for above question: Agar ander wala fruitName function hata de aur bahar wala rehne
de to kya bahar wala call hoga ya error aaega?
Que How can you upgrade/downgrade the version of ReactJS that you are using in your project?
Ans To understand it consider this example given below in this image code
Is code ko dekh ke lagega ki button click karne se Apple ki jagah banana dikhega Page pe. Par aisa
hoga nahi, fruit ki value change hoke apple se banana to ho jaegi par page pe abhi bhi apple hi
dikhega. Kyoki ReactJS me UI me changes tabhi dikhte hai jab vo component rerender hota hai.
To is component ko render karne ke liye hame state variable banake usko change karna padega
because state variable me changes hone par hi component re render hoga.
Que But wait, What is a state?
Ans
The useState Hook is a React Hook that lets you add state to functional components.
In short:
● It gives your functional component a way to "remember" data that can change over time.
● It returns an array with two items:
1. The current state value.
2. A function to update that state value.
● When you call the update function, React re-renders the component with the new state.
Ans The difference is state is used when we have to reflect the changes in a variable on UI. On the
other hand, a variable is used to store some value.
Ans
Ans Core JavaScript ke ander ye functionality banane ke liye ham element ka style change kr dete hai
Hide ke liye display=”none” aur show ke liye display=”block” par ReactJS me ye Ais =e nahi
implement hota hai, Yaha pe ham state ka use karte hai aur conditionaly items ko render karwate
hai. Iske steps hai as follows:
● Define state
● Update state on button click
● Hide and show component
Example:
The condition display?<h1>Deepak Singh</h1> is called Conditional Rendering. When you will
click on the button The Heading will disappear from UI and if you click again it will reappear
Que How to use this conditional Rendering for multiple conditions so that different elements are
rendered for different conditions.
Ans
Que What is a prop in ReactJS?
Ans Jab bhi ReactJS me hame data ek component se dusre component me pass karna hota hai tab ham
props use karte hai.
[Link] [Link]
Isko karna ka ek aur tarika hai which is shown in the image below
Ans They are just like default arguments in a function. Agar props pass nhi kia gaya to default jo diye
honge vo pass ho jaega
Is example me dekho <User/> component me prop nahi pass kiya gaya par default wala use ho rha
Ans
[Link]
[Link]
Output
To isme hua ye hai ki <Wrapper> tag jo ki [Link] me hai (isko self closing nahi rakhna hai) uske
ander kuch JSX code likha hai ap iske pass krna hai. To [Link] me prop {Children} likh ke
receive karo to pass kia hua JSX recieve ho jaega [Link] me.
Ans Steps:
● Make input field
● Define State
● Get input field value in State
● Display Value
● Clear Input field and value
Ans A controlled component is a form whose input field value is controlled by React’s state. Form hona
important nahi hai bas input field ki value state ke sath control honi chaiye.
Here’s how it works:
● Store input field value in a State
● Use change Handler with input field
● Value attribute attached with state
Basically hame Form bharne ke turant baad Input fields ki values chaiye jitne bhi input fields ho aur
aisa ham state ka use karke kar sakte hai
Que Handling Checkboxes in ReactJS through state
To isme ham kya kar rhe h ki check karne par state ki help se array me checkbox ki value dal rhe h
aur uncheck pe array se nikal rhe hai using state
Que Handle Radio and dropdown using state
Ans Return statement ke ander map use krna padega uske bahar agar loop karna hai to normal for ya
while loop use kar sakte ho.
Agar simple data hai to simple array banalo agar complex hai to array of objects bana sakte ho
Is example me <tr key={[Link]}> usko uniquely identify karne ke liye diya gaya hai, so that error na
aaye
Output
Que Reusing a component in Loop
Output
Que Make A digital Clock in ReactJS us clock ke ander ka time wala text should changeable. Implement
the color changing functionality using props
Ans
[Link] [Link]
[Link] → Shows each college and sends students to the next component
We had an array of objects and inside each object we had an array (nested)
Ans In older versions of react we were using class based components before 16.8 version of react to un
class based components me ham state vagaira directly use kr sakte the. But ab functional
components use hote hai aur usme to directly state vagaira use kar nahi sakte isly states vagaira
use karne ke liye Hooks ko introduce kiya gaya.
Que Explain useEffect hook in ReactJS
Ans useEffect hook ka use ham tab kar sakte hai jab
● Remove side effects from inside component
● Remove side effects from outside component
● Fetch data
● Use as life cycle method
● For DOM Manipulation
Ans Suppose koi component ke ander koi function hai to vo function bar bar call hoga jab vo component
re render hoga bar bar.
Jese is example me state update hone pe ye test function bar bar call hoga kyoki ham jab bhi
counter ko update karenge poora component re render hoga aur jab re render hoga to ye
function bhi call hoga bar bar. To yahi hai side effect of state aur isiko hame handle karna hai
To isko ham handle karenge using useState hook ab ye bas ek baar call hoga
Que More about useEffect hook
Ans
To dependency array me agar state dete ho joi bhi to function tabhi call hoga jab vo state update
hoga varna agar kuch nahi dete ho dependency array me to function bas ek baar call hoga
Ans
[Link] [Link]
Isme jab bhi count prop update hoga to fir se side effect occur hoga aur handleCounter function call
hoga isliye useeffect me us function ko call kiya isse vo ek bar call hoga. Agar chahte ho har bar
props updation pe call ho to usko useEffect ke dependency array me daal ke use karo
Ans
Que How to handle React life cycle methods Through ReactJS useEffect Hook?
Ans Iska matlab ye hai ki if you want ki kisi component ke mount/update/unmount hone pe kuch react
code execute ho to vo ham kar sakte hai useEffect() hook use karke
Que How many ways are there to add styling in ReactJS ?
Ans
Jab han React me inline styling use kar rhe hote hai to usko JavaScript objects se style karte hai aur
ek object me properties alag karne ke liye comma lagate hai semicolon nahi aur camelCase use
karte hai
Ans
Suppose you want to create a user profile card
like on the left side and want to change the
background color and text color through the
given buttons dynamically, then you can
change it using state variables and and
changing the Inline CSS of the profile card
dynamically through the state variables
Que How to use External Styling in ReactJS?
Ans We need CSS modules to solve common problems that arise with global CSS, such as naming
conflicts, code complexity, and style isolation issues. They provide a way to scope CSS locally to a
component, ensuring that styles for one component do not unintentionally affect other parts of the
application.
Here h1 with class heading in [Link] wont be styled but it will be styled in [Link]
Ans Styled-components is a library that allows you to write CSS-in-JS, enabling you to create reusable
and encapsulated components with their own styles. It uses tagged template literals in JavaScript
and the power of ES6 to bridge the gap between components and styling.
There are two ways you can write css in styled component
Example usage and output
[Link] Output
Example:
[Link] Output
Note: UseRef is for mutable, not reactive values. A useRef object is just a plain javaScript object
with a .current property. React does not monitor this object for changes. When you update it, you
are doing it so imperatively without invoking React’s rendering engine.
Ans We know that a controlled component is a form (generally not always) whose input field value is
controlled by React’s state. Similarly agar values ko directly DOM se uthae not through state then
that will be called an uncontrolled component.
Here is an example of An uncontrolled component form using querySelector
[Link] [Link]
Ans Jese useRef() se kisi element ka reference lete the na vese hi agar reference alag component me ho
aur input filed (jiska reference hai) vo alag component me tab forward Ref Use karte hai
[Link] [Link]
Que Explain useFormStatus hook in ReactJS
Ans
The useFormStatus hook gives you the submission status of a parent <form> from within a
component. It returns an object with a single property, pending, which is a boolean value.
This hook is designed to simplify how you manage loading states for submit buttons and other UI
elements inside a form. It eliminates the need to pass state down from the form component to its
children, a pattern known as "prop drilling."
Example:
Que Explain useTransition hook in ReactJS
Ans Agar kisi button ko disable karna ho for some time then we can use this isko ham bina
useTransition hook se bhi implement kar sakte hai
Lekin agar useTransition use kare to hame pending ko true false set nahi krna padega vo apne aap
ho jaega
Que What is the difference in useFormStatus hook and useTransition hook?
Ans useFormStatus hook ke liye fom ka hona must hai aur form ke submit button pe use karte hai but
useTransition ke liye form nahi chiaye kisi bhi button pe laga sakte hai isko
Ans Pure component ek aisa component hai jo apne bahar ke cheejo ko change nahi karta hai
For example agar koi variable component ke bahar hai to usko use krna chahte ho component me
ander to directly us variable ko mat use kro usko component me props ke through use kro
Is example me dekho count variable jo ki component ke bahar hai usko directly na use krke props
ke though use kar rhe hai
Example:
Que What is the derived state in ReactJS?
Ans ● State that is calculated or derived from other state values or props within your component
● Derived state can be a variable
● No need to use extra state we can use constants or variables
In this example the variables total, last and unique are derived states
Que What is meant by the Lifting Up state? OR Sharing state between components?
Ans Lifting state up is a technique in React where you move the state from a child component to its
closest common parent component. This allows multiple sibling components to share data.
Is example me [Link] me state na paas krke parent me kardi (lift up state) aur parent se fir
required cheeje props ke through children me pass kar di
Que How to update objects which are being used as state variables?
Ans App directly update nahi kr sakte objects ko jese ab tak state variables ko karte aa rhe hai aur aisa
islye hai because
"The reason we cannot directly mutate an object or array in React state, unlike a simple variable, is
because of React's mechanism for detecting changes and triggering re-renders, known as the
reconciliation process.
This mechanism relies on a concept called shallow comparison of the state.
In JavaScript, objects and arrays are non-primitive data types, meaning they are stored by reference
(a memory address), not by value.
● Primitives (e.g., number, string): When you update a number from 5 to 6, React compares
the two values directly (5 !== 6). The change is obvious.
● Objects/Arrays: When you call the state setter function (like setData), React compares the
new object's memory address with the old object's memory address.
If we mutate the object directly—for instance, [Link] = 'New Name'—and then pass that
same object back to the setter function (setData(data)):
To correctly inform React that a change has occurred, we must use techniques like the spread
operator (...) to create a brand-new object or array. This forces a change in the memory address,
ensuring React sees oldReference !== newReference and triggers the necessary re-render.
For example, to update a nested object, we must spread at every level of the object structure to
ensure every parent object gets a new reference on its way up to the root state.
Ans
Que Explain useActionState hook in ReactJS
Ans
Ans
A Fragment in React is a feature that lets you group multiple children elements without adding an
extra node to the DOM.
In React, every component's render or return method must return a single root element. If you
have multiple elements, you traditionally had to wrap them in a redundant parent <div>.
Fragments solve this issue by serving as a logical wrapper that does not produce any HTML
output.
React components must return a single JSX element. This constraint often forced developers to
unnecessarily nest their elements inside a surrounding <div>:
Fragments allow you to group the elements without introducing the extra div. There are two ways
to use them:
You can explicitly import and use [Link]. This syntax is required if you need to pass the
key prop (e.g., when rendering a list of fragments).
function Columns() {
return (
<[Link]> {/* This element is invisible in the final DOM */}
<td>Hello</td>
<td>World</td>
</[Link]>
);
}
For simple grouping where no props are needed, you can use the empty tag syntax, which is the
most common way developers use fragments:
function Columns() {
return (
<> {/* This is the shorthand for <[Link]> */}
<td>Hello</td>
<td>World</td>
</>
);
}
Que What are the rules for creating custom hooks in React JS?
To context API use karke ham college component se directly data subject component me bhej sakte
hai without prop drilling(jo uper data pass-data pass kr rhe the vahi hai prop drilling)
Ans createContext:
To initiate context API
Provider:
Used for updating or providing data (uper wale ex me ye college component pe lagega)
useContext:
Get data from context API (uper wale ex me student component pe lagega ya jaha bhi chaiye vaha)
createContext ek alag file rhegi jo connected rhegi jaha pe provide aur usecontext hai vaha se
BrowserRouter ka wrapper banan hota hai jaha pe bhi react-router use krna chahte ho to behtar ye
hoga ki isko [Link] file me rakho top level pe
Routes
Jitne bhi pages banenge vo iske ander aaenge i.e responsible for rendering appropriate component
Based on the current URL
Link
A link to navigate from one page to another page.
[Link] [Link]
These Home, About and Login are separate jsx pages
Ans
[Link] [Link]
Agar koi route match nahi hua URL me to last wala chal jaega
[Link] [Link]
Que What is the difference between <Link> and <NavLink>
Ans Link
Kya krta hai? Ye ek component se dusre component pe navigate karta hai bina reload kiye
styling? Sirf static styling leta hai, yani jo styling doge vohi hamesha applied rhega
NavLink
Ye kya hai? Ye <Link> hi hai par isme active state recognize karne ka feature hai
Kya krta hai? Jab iska to path (destination) current URL se match karta hai (matlab aap abhi
uss page par hain), toh yeh automatically ek active class add kar deta hai.
styling? Yeh dynamic styling ke liye best hai. Yeh aapko ek function deta hai jahaan aap
check kar sakte hain ki isActive prop (property) true hai ya false.
Ans Ye wala jo Navigation Bar banaye the na, agar chahte ho ki ye bar Home, Login, About pe dikhe par
College wale pe na Dikhe to aise case me ham Layout Route use karte hai
To uske liye ye changes karo
[Link]
[Link]
[Link]
Que What is index route
Ans Ye tab use karte hai jab kisi route ko by default show krna hota hai.
For example, is page me manlo ham chahte hai student wala route show ho by default r=then we
can set it index route
Agar route prefix use kiya To path aise likha jata hai Full URL kya banega
Agar uper Navbar wale example me suppose login pe lagana ho prefix the changes will be
In [Link]
<li><Link className="link" to="/in/user/login">Login</Link></li>
In [Link]
Que What are Dynamic Routes?
Ans
Jo ye dynamic url change wala item h isko useParams se access kar sakte hai
Ans API woh zariya hai jo ek software ko dusre software se data/service lene aur dene mein help karta hai
Que What are API methods?
Ans
Method Desired Action (Request) One-Liner Description
Ans Use a state to check to check whether all the data from the API has come or not
Que Integrate POST method API
Ans
Ans
Ans
Que Kisi form ke input values me by default data kise fill kare?
Ans useParams se values nikal ke state variable me store karlo aur input tag ki value vo state variable
set krdo
Que How to use the useActionState hook in ReactJS for Validation of input fields and why should you
use it when you can apply validation manually with hardcode?
Action
Isko fetch krna pasta hai form ke action attribute ke sath
Pending
For validation and error handling
Ans It is an alternative to useState hook. Par isko har jagah nahi use krna chaiye kyoki iska code thoda
bada aur heavy hota hai
Ans
Lazy loading in React is a technique used to defer the loading of component code until the
moment it's actually needed, rather than loading everything upfront. This is often called code
splitting.
It's primarily an optimization technique that helps reduce the initial bundle size of your application,
leading to faster initial page load times, especially important for Single-Page Applications (SPAs).
Ans
Que
Ans
Que
Ans
Que
Ans
Que
Ans
Que
Ans
Que
Ans
Que
Ans