Skip to content

Commit dd22bae

Browse files
committed
How React works and hooks questions
1 parent 1c0ac5b commit dd22bae

File tree

1 file changed

+78
-4
lines changed

1 file changed

+78
-4
lines changed

README.md

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6047,15 +6047,88 @@ Technically it is possible to write nested function components but it is not sug
60476047
60486048
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.
60496049
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.
60516051
60526052
**[⬆ Back to Top](#table-of-contents)**
60536053
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+
60556120
60566121
**[⬆ Back to Top](#table-of-contents)**
60576122
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.
6125+
6126+
| Feature | `useState` | `useReducer` |
6127+
|-----------------------|--------------------------------------|---------------------------------------|
6128+
| State complexity | Simple (one variable or flat object) | Complex, multi-part or deeply nested |
6129+
| Update style | Direct (e.g. `setState(x)`) | Through actions (e.g. `dispatch({})`) |
6130+
| Update logic | In component | In reducer function |
6131+
| Reusability & testing | Less reusable | Highly reusable & testable |
60596132
60606133
**[⬆ Back to Top](#table-of-contents)**
60616134
@@ -6267,8 +6340,9 @@ Technically it is possible to write nested function components but it is not sug
62676340
const [count, setCount] = useState(0);
62686341
```
62696342
This happens immediately during rendering.
6270-
However, the state update function (**`**setState**`**) is asynchronous in the sense that it doesn't update the state immediately.
6343+
However, the state update function (**setState**) is asynchronous in the sense that it doesn't update the state immediately.
62716344
React **batches** updates and applies them before the next render. You won’t see the updated value immediately after calling `setState`.
6345+
62726346
**Example:**
62736347
```js
62746348
const [count, setCount] = useState(0);

0 commit comments

Comments
 (0)