Skip to content

Commit d166533

Browse files
committed
Add useHook and its usage question
1 parent dc2e82c commit d166533

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6228,7 +6228,8 @@ Technically it is possible to write nested function components but it is not sug
62286228
```
62296229
62306230
**[⬆ 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?
62326233
62336234
The `useState` hook accepts different types of values.
62346235
@@ -6245,6 +6246,7 @@ Technically it is possible to write nested function components but it is not sug
62456246
setUser(prev => ({ ...prev, name: 'Sudheer' })); //correct way
62466247
```
62476248
**[⬆ Back to Top](#table-of-contents)**
6249+
62486250
276. ### What happens if you call `useState` conditionally?
62496251
As per rules of React Hooks, hooks must be called unconditionally. For example, if you conditionally call it:
62506252
```js
@@ -6255,6 +6257,7 @@ Technically it is possible to write nested function components but it is not sug
62556257
62566258
React will throw a runtime error because it **relies on the order of Hook calls**, and conditional logic breaks that order.
62576259
**[⬆ Back to Top](#table-of-contents)**
6260+
62586261
277. ### Is useState Synchronous or Asynchronous?
62596262
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.
62606263
```js
@@ -6376,6 +6379,22 @@ Technically it is possible to write nested function components but it is not sug
63766379
63776380
**[⬆ Back to Top](#table-of-contents)**
63786381
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+
63796398
## Old Q&A
63806399
63816400
1. ### Why should we not update the state directly?

0 commit comments

Comments
 (0)