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
+24-1Lines changed: 24 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -6171,7 +6171,9 @@ Technically it is possible to write nested function components but it is not sug
6171
6171
6172
6172
272. ### Can Hooks be used in class components?
6173
6173
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
+
6174
6175
**[⬆ Back to Top](#table-of-contents)**
6176
+
6175
6177
273. ### What is an updater function? Should an updater function be used in all cases?
6176
6178
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**.
6177
6179
@@ -6228,7 +6230,8 @@ Technically it is possible to write nested function components but it is not sug
6228
6230
```
6229
6231
6230
6232
**[⬆ 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?
6232
6235
6233
6236
The `useState` hook accepts different types of values.
6234
6237
@@ -6245,6 +6248,7 @@ Technically it is possible to write nested function components but it is not sug
6245
6248
setUser(prev=> ({ ...prev, name:'Sudheer' })); //correct way
6246
6249
```
6247
6250
**[⬆ Back to Top](#table-of-contents)**
6251
+
6248
6252
276. ### What happens if you call `useState` conditionally?
6249
6253
As per rules of React Hooks, hooks must be called unconditionally. For example, if you conditionally call it:
6250
6254
```js
@@ -6254,7 +6258,9 @@ Technically it is possible to write nested function components but it is not sug
6254
6258
```
6255
6259
6256
6260
React will throw a runtime error because it **relies on the order of Hook calls**, and conditional logic breaks that order.
6261
+
6257
6262
**[⬆ Back to Top](#table-of-contents)**
6263
+
6258
6264
277. ### Is useState Synchronous or Asynchronous?
6259
6265
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
6266
```js
@@ -6376,6 +6382,23 @@ Technically it is possible to write nested function components but it is not sug
6376
6382
6377
6383
**[⬆ Back to Top](#table-of-contents)**
6378
6384
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
+
6379
6402
## Old Q&A
6380
6403
6381
6404
1. ### Why should we not update the state directly?
0 commit comments