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: src/content/learn/separating-events-from-effects.md
+166-5Lines changed: 166 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1081,7 +1081,7 @@ Há um pequeno problema com essa interface do usuário. Você pode notar que, se
1081
1081
1082
1082
<Hint>
1083
1083
1084
-
Parece que o Efeito que configura o timer "reage" ao valor de `increment`. A linha que usa o valor atual de `increment` para chamar `setCount` realmente precisa ser reativa?
1084
+
Parece que o Efeito que configura o timer "reage" ao valor de `increment`. A linha que usa o valor atual de `increment` para chamar `setCount` realmente precisa ser reativado?
1085
1085
1086
1086
</Hint>
1087
1087
@@ -1107,6 +1107,75 @@ Parece que o Efeito que configura o timer "reage" ao valor de `increment`. A lin
The issue is that the code inside the Effect uses the `increment` state variable. Since it's a dependency of your Effect, every change to `increment` causes the Effect to re-synchronize, which causes the interval to clear. If you keep clearing the interval every time before it has a chance to fire, it will appear as if the timer has stalled.
1154
+
1155
+
To solve the issue, extract an `onTick` Effect Event from the Effect:
@@ -1150,6 +1219,98 @@ export default function Timer() {
1150
1219
button { margin:10px; }
1151
1220
```
1152
1221
1222
+
</Sandpack>
1223
+
Since `onTick` is an Effect Event, the code inside it isn't reactive. The change to `increment` does not trigger any Effects.
1224
+
1225
+
</Solution>
1226
+
1227
+
#### Fix a non-adjustable delay {/*fix-a-non-adjustable-delay*/}
1228
+
1229
+
In this example, you can customize the interval delay. It's stored in a `delay` state variable which is updated by two buttons. However, even if you press the "plus 100 ms" button until the `delay` is 1000 milliseconds (that is, a second), you'll notice that the timer still increments very fast (every 100 ms). It's as if your changes to the `delay` are ignored. Find and fix the bug.
1230
+
1231
+
<Hint>
1232
+
Code inside Effect Events is not reactive. Are there cases in which you would _want_ the `setInterval` call to re-run?
0 commit comments