Skip to content

Commit 1c0ac5b

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

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6171,7 +6171,9 @@ Technically it is possible to write nested function components but it is not sug
61716171
61726172
272. ### Can Hooks be used in class components?
61736173
No, Hooks cannot be used inside class components. They are specially designed for function components. This is because hooks depend on the sequence in which they are called during a component’s render, something that's only guaranteed in functional components. However, both class and function components can coexist in the same application.
6174+
61746175
**[⬆ Back to Top](#table-of-contents)**
6176+
61756177
273. ### What is an updater function? Should an updater function be used in all cases?
61766178
An **updater function** is a form of `setState` where you pass a **function** instead of a direct value. This function receives the **previous state** as an argument and returns the **next state**.
61776179
@@ -6228,7 +6230,8 @@ Technically it is possible to write nested function components but it is not sug
62286230
```
62296231
62306232
**[⬆ Back to Top](#table-of-contents)**
6231-
275. ### What types of values can `useState` hold?
6233+
6234+
275. ### What types of values can `useState` hold?
62326235
62336236
The `useState` hook accepts different types of values.
62346237
@@ -6245,6 +6248,7 @@ Technically it is possible to write nested function components but it is not sug
62456248
setUser(prev => ({ ...prev, name: 'Sudheer' })); //correct way
62466249
```
62476250
**[⬆ Back to Top](#table-of-contents)**
6251+
62486252
276. ### What happens if you call `useState` conditionally?
62496253
As per rules of React Hooks, hooks must be called unconditionally. For example, if you conditionally call it:
62506254
```js
@@ -6254,7 +6258,9 @@ Technically it is possible to write nested function components but it is not sug
62546258
```
62556259
62566260
React will throw a runtime error because it **relies on the order of Hook calls**, and conditional logic breaks that order.
6261+
62576262
**[⬆ Back to Top](#table-of-contents)**
6263+
62586264
277. ### Is useState Synchronous or Asynchronous?
62596265
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.
62606266
```js
@@ -6376,6 +6382,23 @@ Technically it is possible to write nested function components but it is not sug
63766382
63776383
**[⬆ Back to Top](#table-of-contents)**
63786384
6385+
279. ### What is `useReducer`? Why do you use useReducer?
6386+
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.
6387+
6388+
The `useReducer`  hook is used when:
6389+
6390+
* The **state is complex**, such as nested structures or multiple related values.
6391+
* State updates depend on the **previous state** and **logic**.
6392+
* You want to **separate state update logic** from UI code to make it cleaner and testable.
6393+
* You’re managing features like:
6394+
* Forms
6395+
* Wizards / Multi-step flows
6396+
* Undo/Redo functionality
6397+
* Shopping cart logic
6398+
* Toggle & conditional UI logic
6399+
6400+
**[⬆ Back to Top](#table-of-contents)**
6401+
63796402
## Old Q&A
63806403
63816404
1. ### Why should we not update the state directly?

0 commit comments

Comments
 (0)