React.Js - Interview
React.Js - Interview
Answer.
2. It allows developers to create large web applications that can update and render
efficiently in response to data changes.
ReactJS Solution: React uses a Virtual DOM, which is an in-memory representation of the real
DOM. React compares changes between the Virtual DOM and the real DOM (using a process
called reconciliation) and only updates the necessary parts of the DOM, resulting in faster
updates and better performance.
2. Component-Based Architecture
Earlier, developers used to write UIs as one big chunk of code, which became hard to manage
as the application grew. This approach made reusability and maintenance difficult.
ReactJS Solution: React introduced the concept of components — small, reusable pieces of
code that define parts of the UI (like buttons, forms, or entire sections). This component-based
architecture allows developers to:
3. State Management
● As applications grow in size, keeping track of user interactions, data changes, and UI
updates can become complex. Managing this state (data) directly with traditional
JavaScript can lead to errors, inconsistency, and unnecessary re-renders.
● ReactJS Solution: React provides a stateful system, where components can manage
their own state. React's efficient re-rendering ensures that when state changes, only the
relevant parts of the UI are updated, making it much easier to manage dynamic content.
4. Declarative Syntax
In traditional JavaScript, managing data flow between different parts of an app could be error-
prone, leading to bugs where data was updated in multiple places.
ReactJS Solution: React enforces unidirectional data flow (data flows in one direction). State
is passed down from parent components to child components as props. This makes it easier to
track data and flow through the app and reduces bugs.
Before React, developers had to rely on different frameworks or libraries for different tasks, such
as routing, state management, and UI components.
ReactJS Solution: React has built a rich ecosystem with libraries like React Router (for
routing), Redux (for state management), and React Native (for building mobile apps). It allows
developers to use these tools alongside React to build complex apps more efficiently.
Answer:
● The Virtual DOM is an in-memory representation of the real DOM elements generated
by React components.
● When a component’s state changes, a new Virtual DOM is created and compared to the
previous one using a process called “reconciliation.”
● Only the differences are then updated in the real DOM, leading to efficient rendering and
improved performance.
Ques 4. What is JSX?
● Answer:
● JSX stands for JavaScript XML. It is a syntax extension for JavaScript that allows
developers to write HTML-like code within JavaScript files.
● JSX makes the code easier to understand and debug.
Answer:
Answer:
● Props (short for properties) are read-only attributes passed from a parent component to a
child component.
● They allow data to flow between components and enable component reusability.
Answer:
● State is an object managed within a component that holds dynamic data influencing the
component’s rendering and behavior.
● Unlike props, state is mutable and can change over time, usually in response to user
actions.
Answer:
1. State:
2. Props:
Answer:
Introduced in React 16.8, Hooks are functions that let developers use state and other React
features in function components without writing class components.
Common Hooks:
Answer:
The useState Hook allows you to add state to function components. It returns an array with two
elements: the current state value and a function to update it.
Ques 11. What is the useEffect Hook?
Answer:
The useEffect Hook lets you perform side effects in function components, such as data fetching,
subscriptions, or manually changing the DOM. It runs after the first render and after every
update by default.
Answer:
1. Virtual DOM enhances performance by reducing direct manipulations of the real DOM,
which are typically slow operations.
2. When a component’s state changes, React creates a new Virtual DOM and compares it to
the previous one (a process called “reconciliation”).
3. It then updates only the parts of the real DOM that have changed, minimizing the number
of manipulations and leading to faster updates.
Answer:
Keys are used in React to help identify unique elements in a list. They improve performance by
allowing React to track changes efficiently when rendering dynamic lists.
Without keys, React may incorrectly update or reorder list items.
Ques 14. What is the difference between controlled and uncontrolled components?
Answer:
Answer:
React Context API is a built-in state management feature that allows data to be shared
globally across components without manually passing props at every level (prop drilling).
It is useful when many components need access to the same global state, such as theme
settings, authentication status, and user preferences.
Answer:
Synthetic Event in React is a wrapper around the native browser event. It normalizes events
across different browsers, ensuring consistent behavior.
React’s synthetic events wrap native events like click, keydown, focus, etc., making them cross-
browser compatible.
Answer:
● Keys in React lists help identify which items have changed, been added, or removed.
● They optimize rendering by allowing React to track elements efficiently, avoiding
unnecessary re-renders and UI inconsistencies.
● Without keys, React may re-render elements incorrectly, causing UI bugs.
● The best practice is to use unique IDs as keys rather than array indices, as index keys can
cause issues when items are added, removed, or reordered.