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
Copy file name to clipboardExpand all lines: README.md
+78-4Lines changed: 78 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6047,15 +6047,88 @@ Technically it is possible to write nested function components but it is not sug
6047
6047
6048
6048
1. Reducers must be pure without mutating the state. That means, same input always returns the same output. These reducers run during rendering time similar to state updater functions. So these functions should not send any requests, schedule time outs and any other side effects.
6049
6049
6050
-
2. Each action should describe a single user interaction eventhough there are multiple changes applied to data. For example, if you "reset" registration form which has many user input fields managed by a reducer, it is suggested to send one "reset" action instead of creating separate action for each fields. The proper ordering of actions should reflect the user interactions in the browser and it helps a lot for debugging purpose.
6050
+
2. Each action should describe a single user interaction even though there are multiple changes applied to data. For example, if you "reset" registration form which has many user input fields managed by a reducer, it is suggested to send one "reset" action instead of creating separate action for each fields. The proper ordering of actions should reflect the user interactions in the browser and it helps a lot for debugging purpose.
6051
6051
6052
6052
**[⬆ Back to Top](#table-of-contents)**
6053
6053
6054
-
265. ### What is useReducer hook? Can you describe its usage?
6054
+
265. ### How does ReactJS work behind the scenes?
6055
+
ReactJS is a powerful JavaScript library for building user interfaces. While it appears simple on the surface, React performs a lot of complex operations behind the scenes to efficiently update the UI. Here's an overview of how it works internally:
6056
+
6057
+
#### **1. Virtual DOM & Component Rendering**
6058
+
6059
+
React doesn't manipulate the real DOM directly. Instead, it uses a **Virtual DOM** — a lightweight JavaScript representation of the UI.
6060
+
6061
+
When a component renders (e.g., `<App />`):
6062
+
6063
+
* React **executes the component function** (e.g., `App()`).
6064
+
* Hooks like `useState` are registered and tracked in order.
6065
+
* React builds a **Virtual DOM tree** from the returned JSX.
6066
+
* This virtual DOM is a **plain JS object** that describes the desired UI.
6067
+
6068
+
This process ensures fast and efficient rendering before React decides how to update the real DOM.
6069
+
6070
+
#### 2. **React Fiber Architecture**
6071
+
6072
+
React’s core engine is called **Fiber**, introduced in React 16. Fiber is a reimplementation of the React reconciliation algorithm with the following capabilities:
6073
+
6074
+
* Breaks rendering work into **units of work** (fiber nodes).
6075
+
* Enables **interruptible rendering** (important for responsiveness).
6076
+
* Supports **priority scheduling** and **concurrent rendering**.
6077
+
6078
+
Each Fiber node represents a component and stores:
6079
+
6080
+
* The component type (function/class).
6081
+
* Props, state, and effects.
6082
+
* Links to parent, child, and sibling fibers.
6083
+
6084
+
#### 3. **Reconciliation (Diffing Algorithm)**
6085
+
6086
+
When state or props change:
6087
+
6088
+
* React re-executes the component to produce a new virtual DOM.
6089
+
* It **compares the new virtual DOM to the previous one** using an efficient diffing algorithm.
6090
+
* React determines the **minimal set of DOM changes** required.
6091
+
6092
+
This process is known as **reconciliation**.
6093
+
6094
+
#### 4. **Commit Phase (Real DOM Updates)**
6095
+
6096
+
Once reconciliation is done:
6097
+
6098
+
* React enters the **commit phase**.
6099
+
* It applies calculated changes to the **real DOM**.
6100
+
* It also runs side effects like `useEffect` or `useLayoutEffect`.
6101
+
6102
+
This is the only time React interacts directly with the browser DOM.
6103
+
6104
+
#### 5. **Hooks and State Management**
6105
+
6106
+
With Hooks (like `useState`, `useEffect`):
6107
+
6108
+
* React keeps an **internal list of hooks per component**.
6109
+
* Hooks are identified by their order in the function.
6110
+
* When state updates occur, React re-renders the component and re-runs the hooks in the same order.
6111
+
6112
+
#### 6. **React Scheduler**
6113
+
6114
+
React uses an internal **Scheduler** to control how updates are prioritized:
6115
+
6116
+
* Urgent tasks like clicks and inputs are processed immediately.
6117
+
* Non-urgent tasks (like data fetching) can be delayed or paused.
6118
+
* This improves responsiveness and allows for **time slicing** in Concurrent Mode.
6119
+
6055
6120
6056
6121
**[⬆ Back to Top](#table-of-contents)**
6057
6122
6058
-
266. ### How do you compare useState and useReducer?
6123
+
266. ### How is `useReducer` Different from `useState`?
6124
+
There are notable differences between `useState` and `useReducer` hooks.
0 commit comments