0% found this document useful (0 votes)
11 views8 pages

React.Js - Interview

ReactJS is an open-source JavaScript library developed by Facebook for building user interfaces, particularly single-page applications. It enhances performance through a Virtual DOM, promotes a component-based architecture for better maintainability, and provides state management and hooks for functional components. Additionally, React's unidirectional data flow and rich ecosystem support efficient app development.

Uploaded by

mrpubg632
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views8 pages

React.Js - Interview

ReactJS is an open-source JavaScript library developed by Facebook for building user interfaces, particularly single-page applications. It enhances performance through a Virtual DOM, promotes a component-based architecture for better maintainability, and provides state management and hooks for functional components. Additionally, React's unidirectional data flow and rich ecosystem support efficient app development.

Uploaded by

mrpubg632
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Ques 1: What is ReactJS?

Answer.

1. ReactJS is an open-source JavaScript library developed by Facebook for building user


interfaces, particularly single-page applications where data changes over time.

2. It allows developers to create large web applications that can update and render
efficiently in response to data changes.

Why do we need ReactJS? Why was it created?

1. Performance Optimization (Virtual DOM)


In traditional JavaScript, directly manipulating the DOM (Document Object Model) can be slow
and inefficient, especially when making frequent updates. Each change would require re-
rendering the entire DOM, which impacts performance.

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:

● Re-use components across different parts of the application.

● Easily manage and update UI elements independently.

● Better maintainability for larger applications.

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

● Writing JavaScript to manipulate the DOM directly (imperative programming) can


quickly become cumbersome and difficult to debug.
● ReactJS Solution: React uses a declarative approach to UI design. Instead of manually
updating the DOM with JavaScript, you describe what the UI should look like based on
the current state. React takes care of the "how" by automatically updating the DOM
when the state changes.

5. Unidirectional Data Flow

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.

6. Ecosystem and Community

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.

Ques 3. Explain the concept of Virtual DOM in React.

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.

Ques 5. How do you create a React component?

Answer:

React components can be created as either function components or class components.

1. Function Component Example:

2. Class Component Example:


Ques 6. What are props in React?

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.

Ques 7. What is state in React?

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.

Ques 8.Differentiate between state and props.

Answer:
1. State:

1. Managed within the component.


2. Mutable and can change over time.
3. Used for dynamic rendering.

2. Props:

1. Passed from parent to child components.


2. Immutable; cannot be modified by the receiving component.
3. Used to pass data and event handlers to child components.

Ques 9. What are React Hooks?

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:

● useState: Manages state in function components.


● useEffect: Performs side effects in function components.
● useContext: Accesses context values without wrapping components.

Ques 10. Explain the useState Hook with an example.

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.

Ques 12. How does the Virtual DOM improve performance?

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.

Ques 13. What is the importance of keys in React lists?

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:

Ques 15. What is React Context API?

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.

Ques 16. What are synthetic events in React?

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.

● handleClick function logs details about the event.


● event is not the native browser event, but a React SyntheticEvent.
● React automatically pools events for performance optimization.

Ques 17: Why is there a need to use keys in Lists?

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.

You might also like