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
@@ -4946,11 +4946,25 @@ class ParentComponent extends React.Component {
4946
4946
4947
4947
**[⬆ Back to Top](#table-of-contents)**
4948
4948
4949
-
217. ### How to fetch data with React Hooks?
4949
+
217. ### What is useEffect hook? How to fetch data with React Hooks?
4950
4950
4951
-
The effect hook called `useEffect` can be used to fetch data from an API and to set the data in the local state of the component with the useState hook’s update function.
4951
+
The `useEffect` hook is a React Hook that lets you perform **side effects** in function components. Side effects are operations that interact with the outside world or system and aren't directly related to rendering UI — such as fetching data, setting up subscriptions, timers, manually manipulating the DOM, etc.
4952
4952
4953
-
Here is an example of fetching a list of react articles from an API using fetch.
4953
+
In function components, useEffect replaces the class component lifecycle methods(`componentDidMount`, `componentDidUpdate` and `componentWillUnmount`) with a single, unified API.
4954
+
4955
+
**Syntax**
4956
+
```js
4957
+
useEffect(() => {
4958
+
// Side effect logic here
4959
+
4960
+
return () => {
4961
+
// Cleanup logic (optional)
4962
+
};
4963
+
}, [dependencies]);
4964
+
```
4965
+
This effect hook can be used to fetch data from an API and to set the data in the local state of the component with the useState hook’s update function.
4966
+
4967
+
Here is an example of fetching a list of ReactJS articles from an API using fetch.
4954
4968
4955
4969
```javascript
4956
4970
importReactfrom"react";
@@ -6862,6 +6876,125 @@ Technically it is possible to write nested function components but it is not sug
6862
6876
6863
6877
**[⬆ Back to Top](#table-of-contents)**
6864
6878
6879
+
287. ### How do reactive dependencies in the useEffect dependency array affect its execution behavior?
6880
+
The `useEffect` hook accepts an optional dependencies argument that accepts an array of reactive values. The **dependency array** determines **when** the effect runs. i.e, It makes `useEffect` _reactive_ to changes in specified values.
2. **With Specific Dependencies:** `**[count, user]**`
6896
+
6897
+
```css
6898
+
useEffect(() => {
6899
+
// runs after initial render
6900
+
// AND whenever `count` or `user` changes
6901
+
}, [count, user]);
6902
+
```
6903
+
6904
+
* Effect runs on **first render**, and
6905
+
* Again **every time** any dependency value changes.
6906
+
6907
+
3. **No Dependency Array (Omitted)**
6908
+
6909
+
```css
6910
+
useEffect(() => {
6911
+
// runs after **every** render
6912
+
});
6913
+
```
6914
+
6915
+
* Effect runs after **every render**, regardless of what changed.
6916
+
* Can lead to **performance issues** if not used carefully.
6917
+
6918
+
React uses shallow comparison of the dependencies. If any value has changed (!==), the effect will re-run.
6919
+
6920
+
**Note:** This hook works well when dependencies are primitives or memoized objects/functions.
6921
+
6922
+
**[⬆ Back to Top](#table-of-contents)**
6923
+
6924
+
288. ### When and how often does React invoke the setup and cleanup functions inside a useEffect hook?
6925
+
6926
+
1. **Setup Function Execution (`useEffect`)**
6927
+
6928
+
The setup function (or the main function) you pass to `useEffect` runs at specific points:
6929
+
6930
+
1. **After the component is mounted** (if the dependency array is empty `[]`)
6931
+
2. **After every render** (if no dependency array is provided)
6932
+
3. **After a dependency value changes** (if the dependency array contains variables)
6933
+
6934
+
2. **Cleanup Function Execution (Returned function from `useEffect`)**
6935
+
6936
+
The cleanup function is called **before the effect is re-executed** and when the component **unmounts**.
6937
+
6938
+
**[⬆ Back to Top](#table-of-contents)**
6939
+
6940
+
289. ### What happens if you return a Promise from useEffect??
6941
+
You should NOT return a Promise from useEffect. React expects the function passed to useEffect to return either nothing (undefined) or a cleanup function (synchronous function). i.e, It does not expect or handle a returned Promise. If you still return a Promise, React will ignore it silently, and it may lead to bugs or warnings in strict mode.
6942
+
6943
+
**Incorrect:**
6944
+
```js
6945
+
useEffect(async () => {
6946
+
awaitfetchData(); // ❌ useEffect shouldn't be async
6947
+
}, []);
6948
+
```
6949
+
**Correct:**
6950
+
```jsx
6951
+
useEffect(() => {
6952
+
constfetchData=async () => {
6953
+
constres=awaitfetch('/api');
6954
+
constdata=awaitres.json();
6955
+
setData(data);
6956
+
};
6957
+
6958
+
fetchData();
6959
+
}, []);
6960
+
```
6961
+
**[⬆ Back to Top](#table-of-contents)**
6962
+
6963
+
289. ### Can you have multiple useEffect hooks in a single component?
6964
+
Yes, multiple useEffect hooks are allowed and recommended when you want to separate concerns.
6965
+
6966
+
```jsx
6967
+
useEffect(() => {
6968
+
// Handles API fetch
6969
+
}, []);
6970
+
6971
+
useEffect(() => {
6972
+
// Handles event listeners
6973
+
}, []);
6974
+
```
6975
+
Each effect runs independently and helps make code modular and easier to debug.
6976
+
6977
+
**[⬆ Back to Top](#table-of-contents)**
6978
+
6979
+
290. ### How to prevent infinite loops with useEffect?
6980
+
Infinite loops happen when the effect updates state that’s listed in its own dependency array, which causes the effect to re-run, updating state again and so on.
6981
+
6982
+
**Infinite loop scenario:**
6983
+
```js
6984
+
useEffect(() => {
6985
+
setCount(count +1);
6986
+
}, [count]); // Triggers again every time count updates
6987
+
```
6988
+
You need to ensure that setState calls do not depend on values that cause the effect to rerun, or isolate them with a guard.
6989
+
```js
6990
+
useEffect(() => {
6991
+
if (count <5) {
6992
+
setCount(count +1);
6993
+
}
6994
+
}, [count]);
6995
+
```
6996
+
**[⬆ Back to Top](#table-of-contents)**
6997
+
6865
6998
## Old Q&A
6866
6999
6867
7000
1. ### Why should we not update the state directly?
@@ -6908,7 +7041,7 @@ Technically it is possible to write nested function components but it is not sug
6908
7041
6909
7042
**[⬆ Back to Top](#table-of-contents)**
6910
7043
6911
-
1. ### How to bind methods or event handlers in JSX callbacks?
7044
+
3. ### How to bind methods or event handlers in JSX callbacks?
6912
7045
6913
7046
There are 3 possible ways to achieve this in class components:
6914
7047
@@ -6956,7 +7089,7 @@ Technically it is possible to write nested function components but it is not sug
6956
7089
6957
7090
**[⬆ Back to Top](#table-of-contents)**
6958
7091
6959
-
2. ### How to pass a parameter to an event handler or callback?
7092
+
4. ### How to pass a parameter to an event handler or callback?
6960
7093
6961
7094
You can use an _arrow function_ to wrap around an _event handler_ and pass parameters:
6962
7095
@@ -6981,13 +7114,13 @@ Technically it is possible to write nested function components but it is not sug
6981
7114
6982
7115
**[⬆ Back to Top](#table-of-contents)**
6983
7116
6984
-
3. ### What is the use of refs?
7117
+
5. ### What is the use of refs?
6985
7118
6986
7119
The _ref_ is used to return a reference to the element. They _should be avoided_ in most cases, however, they can be useful when you need a direct access to the DOM element or an instance of a component.
6987
7120
6988
7121
**[⬆ Back to Top](#table-of-contents)**
6989
7122
6990
-
4. ### How to create refs?
7123
+
6. ### How to create refs?
6991
7124
6992
7125
There are two approaches
6993
7126
@@ -7036,7 +7169,7 @@ Technically it is possible to write nested function components but it is not sug
7036
7169
7037
7170
**[⬆ Back to Top](#table-of-contents)**
7038
7171
7039
-
5. ### What are forward refs?
7172
+
7. ### What are forward refs?
7040
7173
7041
7174
_Ref forwarding_ is a feature that lets some components take a _ref_ they receive, and pass it further down to a child.
7042
7175
@@ -7054,7 +7187,7 @@ Technically it is possible to write nested function components but it is not sug
7054
7187
7055
7188
**[⬆ Back to Top](#table-of-contents)**
7056
7189
7057
-
6. ### Which is preferred option with in callback refs and findDOMNode()?
7190
+
8. ### Which is preferred option with in callback refs and findDOMNode()?
7058
7191
7059
7192
It is preferred to use _callback refs_ over `findDOMNode()` API. Because `findDOMNode()` prevents certain improvements in React in the future.
7060
7193
@@ -7092,7 +7225,7 @@ Technically it is possible to write nested function components but it is not sug
7092
7225
7093
7226
**[⬆ Back to Top](#table-of-contents)**
7094
7227
7095
-
7. ### Why are String Refs legacy?
7228
+
9. ### Why are String Refs legacy?
7096
7229
7097
7230
If you worked with React before, you might be familiar with an older API where the `ref` attribute is a string, like `ref={'textInput'}`, and the DOM node is accessed as `this.refs.textInput`. We advise against it because _string refs have below issues_, and are considered legacy. String refs were **removed in React v16**.
7098
7231
@@ -7121,7 +7254,7 @@ Technically it is possible to write nested function components but it is not sug
7121
7254
7122
7255
**[⬆ Back to Top](#table-of-contents)**
7123
7256
7124
-
8. ### What are the different phases of component lifecycle?
7257
+
10. ### What are the different phases of component lifecycle?
7125
7258
7126
7259
The component lifecycle has three distinct lifecycle phases:
7127
7260
@@ -7149,7 +7282,7 @@ Technically it is possible to write nested function components but it is not sug
7149
7282
7150
7283
**[⬆ Back to Top](#table-of-contents)**
7151
7284
7152
-
9. ### What are the lifecycle methods of React?
7285
+
11. ### What are the lifecycle methods of React?
7153
7286
7154
7287
Before React 16.3
7155
7288
@@ -7172,7 +7305,7 @@ Technically it is possible to write nested function components but it is not sug
7172
7305
7173
7306
**[⬆ Back to Top](#table-of-contents)**
7174
7307
7175
-
10. ### How to create props proxy for HOC component?
7308
+
12. ### How to create props proxy for HOC component?
7176
7309
7177
7310
You can add/edit props passed to the component using _props proxy_ pattern like this:
7178
7311
@@ -7195,7 +7328,7 @@ Technically it is possible to write nested function components but it is not sug
7195
7328
7196
7329
**[⬆ Back to Top](#table-of-contents)**
7197
7330
7198
-
11. ### What is context?
7331
+
13. ### What is context?
7199
7332
7200
7333
_Context_ provides a way to pass data through the component tree without having to pass props down manually at every level.
7201
7334
@@ -7207,7 +7340,7 @@ Technically it is possible to write nested function components but it is not sug
7207
7340
7208
7341
**[⬆ Back to Top](#table-of-contents)**
7209
7342
7210
-
12. ### What is the purpose of using super constructor with props argument?
7343
+
14. ### What is the purpose of using super constructor with props argument?
7211
7344
7212
7345
A child class constructor cannot make use of `this` reference until the `super()` method has been called. The same applies to ES6 sub-classes as well. The main reason for passing props parameter to `super()` call is to access `this.props` in your child constructors.
7213
7346
@@ -7247,7 +7380,7 @@ Technically it is possible to write nested function components but it is not sug
7247
7380
7248
7381
**[⬆ Back to Top](#table-of-contents)**
7249
7382
7250
-
13. ### How to set state with a dynamic key name?
7383
+
15. ### How to set state with a dynamic key name?
7251
7384
7252
7385
If you are using ES6 or the Babel transpiler to transform your JSX code then you can accomplish this with _computed property names_.
7253
7386
@@ -7259,7 +7392,7 @@ Technically it is possible to write nested function components but it is not sug
7259
7392
7260
7393
**[⬆ Back to Top](#table-of-contents)**
7261
7394
7262
-
14. ### What would be the common mistake of function being called every time the component renders?
7395
+
16. ### What would be the common mistake of function being called every time the component renders?
7263
7396
7264
7397
You need to make sure that function is not being called while passing the function as a parameter.
0 commit comments