You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -996,28 +996,37 @@ class ParentComponent extends React.Component {
996
996
997
997
`SyntheticEvent` is a cross-browser wrapper around the browser's native event. Its API is same as the browser's native event, including `stopPropagation()` and `preventDefault()`, except the events work identically across all browsers. The native events can be accessed directly from synthetic events using `nativeEvent` attribute.
998
998
999
-
Let's take an example of BookStore title search component with the ability to get all native event properties
999
+
Let's take an example of `BookStore` title search component with the ability to get all native event properties
You can use either _if statements_ or _ternary expressions_ which are available from JSto conditionally render expressions. Apart from these approaches, you can also embed any expressions in JSX by wrapping them in curly braces and then followed by JS logical operator `&&`.
1029
+
You can use either _if statements_ or _ternary expressions_ which are available in JS(and JSX in React) to conditionally execute or render expressions. Apart from these approaches, you can also embed any expressions in JSX by wrapping them in curly braces and then followed by JS logical operator `&&`. It is helpful to render elements conditionally within a single line and commonly used for concise logic, especially in JSX rendering.
1021
1030
1022
1031
```jsx harmony
1023
1032
<h1>Hello!</h1>;
@@ -1049,6 +1058,10 @@ class ParentComponent extends React.Component {
1049
1058
<li key={index}>{todo.text}</li>
1050
1059
));
1051
1060
```
1061
+
**Benefits of key:**
1062
+
* Enables React to **efficiently update and re-render** components.
1063
+
* Prevents unnecessary re-renders by **reusing** components when possible.
1064
+
* Helps **maintain internal state**of list items correctly.
1052
1065
1053
1066
**Note:**
1054
1067
@@ -1062,53 +1075,71 @@ class ParentComponent extends React.Component {
1062
1075
1063
1076
16. ### What is Virtual DOM?
1064
1077
1065
-
The _Virtual DOM_ (VDOM) is an in-memory representation of _Real DOM_. The representation of a UI is kept in memory and synced with the "real" DOM. It's a step that happens between the render function being called and the displaying of elements on the screen. This entire process is called _reconciliation_.
1078
+
The _Virtual DOM_ (VDOM) is a lightweight, in-memory representation of _Real DOM_ used by libraries like React to optimize UI rendering. The representation of a UI is kept in memory and synced with the "real" DOM. It's a step that happens between the render function being called and the displaying of elements on the screen. This entire process is called _reconciliation_.
1066
1079
1067
1080
**[⬆ Back to Top](#table-of-contents)**
1068
1081
1069
1082
17. ### How Virtual DOM works?
1070
1083
1071
-
The _Virtual DOM_ works in three simple steps.
1072
-
1073
-
1. Whenever any underlying data changes, the entire UI is re-rendered in Virtual DOM representation.
1074
-
1075
-

1076
-
1077
-
2. Then the difference between the previous DOM representation and the new one is calculated.
1084
+
The _Virtual DOM_ works in five simple steps.
1085
+
**1. Initial Render**
1086
+
When a UI component renders for the first time, it returns JSX. React uses this structure to create a Virtual DOM tree, which is a lightweight copy of the actual DOM. This Virtual DOM is then used to build and render the Real DOM in the browser.
1078
1087
1079
-

1088
+
**2. State or Props Change**
1089
+
When the component's state or props change, React creates a new Virtual DOM reflecting the updated UI. However, it doesn't immediately update the Real DOM; instead, it works in memory to prepare for an efficient update.
1090
+
1091
+

1080
1092
1081
-
3. Once the calculations are done, the real DOM will be updated with only the things that have actually changed.
1093
+
**3. Diffing Algorithm**
1094
+
React then compares the new Virtual DOM with the previous one using a process called diffing. It determines what has changed between the two versions and identifies the minimal set of updates needed.
1095
+
1096
+

1082
1097
1098
+
**4. Reconciliation**
1099
+
Based on the diffing results, React decides which parts of the Real DOM should be updated. It avoids re-rendering the entire DOM and instead updates only the elements that actually changed.
1100
+
1083
1101

1084
1102
1103
+
**5. Efficient DOM Updates**
1104
+
This entire process—working with the Virtual DOM, diffing, and selective updating—makes the UI rendering much faster and more efficient than manipulating the Real DOM directly.
1105
+
1085
1106
**[⬆ Back to Top](#table-of-contents)**
1086
1107
1087
1108
18. ### What is the difference between Shadow DOM and Virtual DOM?
1088
1109
1089
1110
The _Shadow DOM_ is a browser technology designed primarily for scoping variables and CSS in _web components_. The _Virtual DOM_ is a concept implemented by libraries in JavaScript on top of browser APIs.
1090
1111
1112
+
The key differences in a table format shown below:
1113
+
1114
+
| Feature | Shadow DOM | Virtual DOM |
1115
+
| --- | --- | --- |
1116
+
| Purpose | Encapsulation for Web Components | Efficient UI rendering |
| DOM Type | Part of real DOM (scoped) | In-memory representation |
1119
+
| Encapsulation | Yes | No |
1120
+
| Use Case | Web Components, scoped styling | UI diffing and minimal DOM updates |
1121
+
1091
1122
**[⬆ Back to Top](#table-of-contents)**
1092
1123
1093
1124
19. ### What is React Fiber?
1094
1125
1095
-
Fiber is the new _reconciliation_ engine or reimplementation of core algorithm in React v16. The goal of ReactFiber is to increase its suitability for areas like animation, layout, gestures, ability to pause, abort, or reuse work and assign priority to different types of updates; and new concurrency primitives.
1126
+
**React Fiber** is the **new reconciliation engine** in React, introduced in React 16. It’s a complete rewrite of React’s core algorithm(old stack-based algorithm) for rendering and updating the UI. Fiber enhances React’s ability to handle **asynchronous rendering**, **prioritized updates**(assign priority to different types of updates), and **interruption**(ability to pause, abort, or reuse work) of rendering work, enabling smoother and more responsive user interfaces.
1096
1127
1097
1128
**[⬆ Back to Top](#table-of-contents)**
1098
1129
1099
1130
20. ### What is the main goal of React Fiber?
1100
1131
1101
1132
The goal of _React Fiber_ is to increase its suitability for areas like animation, layout, and gestures. Its headline feature is **incremental rendering**: the ability to split rendering work into chunks and spread it out over multiple frames.
1102
1133
1103
-
_from documentation_
1104
-
1105
1134
Its main goals are:
1106
1135
1107
-
1. Ability to split interruptible work in chunks.
1108
-
2. Ability to prioritize, rebase and reuse work in progress.
1109
-
3. Ability to yield back and forth between parents and children to support layout in React.
1110
-
4. Ability to return multiple elements from render().
1111
-
5. Better support for error boundaries.
1136
+
* **Incremental Rendering** – Breaks work into chunks for smoother updates.
1137
+
* **Interruptible Rendering** – Pauses and resumes rendering to keep the UI responsive.
* **Suspense Support** – Allows waiting for async data before rendering.
1142
+
* **Improved DevTools** – Enables better debugging and performance tracking.
1112
1143
1113
1144
**[⬆ Back to Top](#table-of-contents)**
1114
1145
@@ -1236,9 +1267,31 @@ class ParentComponent extends React.Component {
1236
1267
**[⬆ Back to Top](#table-of-contents)**
1237
1268
1238
1269
23. ### What is the difference between createElement and cloneElement?
1270
+
Both `React.createElement` and `React.cloneElement` are used to work with React elements, but they serve different purposes.
1239
1271
1240
-
JSX elements will be transpiled to `React.createElement()` functions to create React elements which are going to be used for the object representation of UI. Whereas `cloneElement` is used to clone an element and pass it new props.
1272
+
#### **createElement:**
1273
+
Creates a new React element from scratch. JSX elements will be transpiled to `React.createElement()` functions to create React elements which are going to be used for the object representation of UI.
@@ -1249,20 +1302,66 @@ class ParentComponent extends React.Component {
1249
1302
1250
1303
25. ### What are Higher-Order Components?
1251
1304
1252
-
A _higher-order component_ (_HOC_) is a function that takes a component and returns a new component. Basically, it's a pattern that is derived from React's compositional nature.
1305
+
A _higher-order component_ (_HOC_) is a function that takes a component and returns a new enhanced component with additional props, behavior, or data. It’s a design pattern based on React’s compositional nature, allowing you to reuse logic across multiple components without modifying their internals.
1253
1306
1254
-
We call them **pure components** because they can accept any dynamically provided child component but they won't modify or copy any behavior from their input components.
1307
+
We consider HOCs **pure components** because they don’t mutate or copy behavior from the original component—they simply **wrap it**, enhance it, and pass through the necessary props. The wrapped component remains decoupled and reusable.
Let's take an example of a `withAuth` higher-order component (HOC) in React. This HOC will check if a user is authenticated and either render the wrapped component if authenticated or redirect (or show a message) if not.
2. Render hijacking (e.g., conditional rendering or layout changes).
1355
+
3. State abstraction and manipulation(e.g., handling form logic).
1356
+
4. Props manipulation(e.g., injecting additional props or filtering them).
1357
+
1358
+
Some of the real-world examples of HOCs in react eco-system:
1359
+
1. connect() from react-redux
1360
+
2. withRouter() from React Router v5
1361
+
3. withTranslation() from react-i18next
1362
+
4. withApollo() from Apollo client
1363
+
5. withFormik from Formik library
1364
+
6. withTheme from styled components
1266
1365
1267
1366
**[⬆ Back to Top](#table-of-contents)**
1268
1367
@@ -7415,7 +7514,52 @@ Technically it is possible to write nested function components but it is not sug
7415
7514
7416
7515
**[⬆ Back to Top](#table-of-contents)**
7417
7516
7418
-
7517
+
307. ### How does React Fiber works? Explain in detail.
7518
+
7519
+
React Fiber is the **core engine** that enables advanced features like **concurrent rendering**, **prioritization**, and **interruptibility** in React. Here's how it works:
7520
+
7521
+
### 1. **Fiber Tree Structure**
7522
+
7523
+
Each component in your app is represented by a **Fiber node** in a tree structure. A Fiber node contains:
7524
+
* Component type
7525
+
* Props & state
7526
+
* Pointers to parent, child, and sibling nodes
7527
+
* Effect tags to track changes (e.g., update, placement)
7528
+
* This forms the **Fiber Tree**, a data structure React uses instead of the traditional call stack.
7529
+
7530
+
### 2. **Two Phases of Rendering**
7531
+
7532
+
#### **A. Render Phase (work-in-progress)**
7533
+
7534
+
* React builds a **work-in-progress Fiber tree**.
7535
+
* It walks through each component (begin phase), calculates what needs to change, and collects side effects (complete phase).
7536
+
* This phase is **interruptible**—React can pause it and resume later.
0 commit comments