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
+20-1Lines changed: 20 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -6228,7 +6228,8 @@ Technically it is possible to write nested function components but it is not sug
6228
6228
```
6229
6229
6230
6230
**[⬆ Back to Top](#table-of-contents)**
6231
-
275. ### What types of values can `useState` hold?
6231
+
6232
+
275. ### What types of values can `useState` hold?
6232
6233
6233
6234
The `useState` hook accepts different types of values.
6234
6235
@@ -6245,6 +6246,7 @@ Technically it is possible to write nested function components but it is not sug
6245
6246
setUser(prev=> ({ ...prev, name:'Sudheer' })); //correct way
6246
6247
```
6247
6248
**[⬆ Back to Top](#table-of-contents)**
6249
+
6248
6250
276. ### What happens if you call `useState` conditionally?
6249
6251
As per rules of React Hooks, hooks must be called unconditionally. For example, if you conditionally call it:
6250
6252
```js
@@ -6255,6 +6257,7 @@ Technically it is possible to write nested function components but it is not sug
6255
6257
6256
6258
React will throw a runtime error because it **relies on the order of Hook calls**, and conditional logic breaks that order.
6257
6259
**[⬆ Back to Top](#table-of-contents)**
6260
+
6258
6261
277. ### Is useState Synchronous or Asynchronous?
6259
6262
The `useState` hook is synchronous, but state updates are asynchronous. When you call `useState()`, it runs synchronously and returns the state variable and setter function as tuple.
6260
6263
```js
@@ -6376,6 +6379,22 @@ Technically it is possible to write nested function components but it is not sug
6376
6379
6377
6380
**[⬆ Back to Top](#table-of-contents)**
6378
6381
6382
+
279. ### What is `useReducer`? Why do you use useReducer?
6383
+
The `useReducer` hook is a React hook used to manage **complex state logic** inside **functional components**. It is conceptually similar to **Redux**. i.e, Instead of directly updating state like with `useState`, you **dispatch an action** to a **reducer function**, and the reducer returns the new state.
6384
+
6385
+
The `useReducer` hook is used when:
6386
+
6387
+
* The **state is complex**, such as nested structures or multiple related values.
6388
+
* State updates depend on the **previous state** and **logic**.
6389
+
* You want to **separate state update logic** from UI code to make it cleaner and testable.
6390
+
* You’re managing features like:
6391
+
* Forms
6392
+
* Wizards / Multi-step flows
6393
+
* Undo/Redo functionality
6394
+
* Shopping cart logic
6395
+
* Toggle & conditional UI logic
6396
+
**[⬆ Back to Top](#table-of-contents)**
6397
+
6379
6398
## Old Q&A
6380
6399
6381
6400
1. ### Why should we not update the state directly?
0 commit comments