Skip to content

Commit bce94db

Browse files
authored
Merge pull request #56 from arvish-codal/26_May
Added notes containing the explanations for incorrect dependency.
2 parents d14f4a9 + 9d3d775 commit bce94db

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

notes.txt

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -773,16 +773,39 @@ NOTE - When strict mode is on, in development, React runs setup and cleanup on e
773773
Read more here --> https://react.dev/reference/react/useEffect#my-effect-runs-twice-when-the-component-mounts
774774
https://stackoverflow.com/questions/53183362/what-is-strictmode-in-react
775775

776+
Some helpful resources :-
777+
* https://developer.mozilla.org/en-US/docs/Web/API/setInterval
778+
* https://developer.mozilla.org/en-US/docs/Web/API/clearInterval
779+
* https://www.htmlallthethings.com/blog-posts/why-do-we-use-const-to-declare-arrow-functions-in-javascript#:~:text=Declaring%20Arrow%20Functions%20with%20Const&text=Constant%20variables%20are%20read%2Donly,from%20accidental%20deletion%20or%20manipulation.
776780

777-
Remember :-
778-
781+
782+
################ IMP ################
783+
784+
VIDEO 54 - FROM 3:20 MIN ;
785+
*****************************
786+
> We need to call the tick method every second.
787+
788+
> This effect is going to fire after every render. If we only want the interval to be set up once on initial render (i.e similar to componentDidMount), we simply pass an empty array
789+
( i.e [] ) as the dependency list.
790+
791+
> If we now take a look at the browser, we can see that the class counter is working as expected but the hook counter is displaying the value of `1` and not incrementing every second.
792+
> In our mind, the problem statement is simple. Create an interval once and destroy it once.
793+
794+
> The empty depndency list ensures that the timer is set only once and return function to destroy the timer that we've created. Why then our counter doesn't work as expected ?
795+
796+
REMEMBER -->
779797
If you think dependency array is a way to specify when you want to rerun the effect, you are going to run into problems.
780798

781799
Instead, dependency array should be thought of as a way to let react know about everything that the effect
782800
must watch for changes.
783801

784-
Some helpful links :-
785-
* https://developer.mozilla.org/en-US/docs/Web/API/setInterval
786-
* https://developer.mozilla.org/en-US/docs/Web/API/clearInterval
787-
* https://www.htmlallthethings.com/blog-posts/why-do-we-use-const-to-declare-arrow-functions-in-javascript#:~:text=Declaring%20Arrow%20Functions%20with%20Const&text=Constant%20variables%20are%20read%2Donly,from%20accidental%20deletion%20or%20manipulation.
788-
* https://react.dev/reference/react/useState#updating-state-based-on-the-previous-state
802+
Therefore, the problem is --> By specifying an empty array, we've basically told React to ignore watching for changes in the count variable.
803+
So, React goes like -> On initial render, the count value is 0 which implies setCount will set it to 0 + 1 = 1 and that'll be rendered in the
804+
browser. Now because the dependency array is set to [], we're telling React that it doesn't have to watch for changes in the count value. Count
805+
value is equal to 1 right now and React will just render that value through the different re-render cycles. If we want React (i.e the `useEffect` hook)
806+
to keep an eye on a variable, we just have to add it to the dependency array (i.e [count]). And our problem will be solved.
807+
808+
BTW, for this example there is another way to get it working without the dependency list. In the tick function, we use the second form of setCount.
809+
We get access to previous count and we set previous count + 1. (Read this to understand more --> https://react.dev/reference/react/useState#updating-state-based-on-the-previous-state)
810+
811+
Now since setCount keeps track of the previous count value, we don't have to specify count as a dependency and that array can again be set to [].

state-hook/src/components/IntervalHookCounter.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ function IntervalHookCounter() {
99
}
1010

1111
useEffect( () => {
12+
console.log('called useEffect here')
1213
const interval = setInterval(tick, 1000)
1314

1415
return () => {

0 commit comments

Comments
 (0)