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
Runtime Error
App.js: _react.experimental_useEffectEvent is not a function (8:37)
5 | const [count, setCount] = useState(0);
6 | const [increment, setIncrement] = useState(1);
7 |
> 8 | const updateCount = useEffectEvent(() => {
^
9 | setCount(c => c + increment);
10 | });
11 |
Therefore, by default, I can't use useEffectEvent for that exercise. I wrote "by default" because maybe it can be used by opening it in CodeSandbox. It looks like an extra effort for me, so I haven't tried it yet.
One of the solution I found for this challenge is using useRef. I think, the key for this challenge is how to avoid passing reactive elements to the Effect. Because useRef somehow can be treated as local variable which can be changed outside of render, and the interval itself is in-between render, I think it's safe to use. I need to make a Ref consistent with increment state. So, I modified each callback function of the increment buttons.
Here is my solution:
import{useState,useEffect,useRef}from'react';exportdefaultfunctionTimer(){const[count,setCount]=useState(0);const[increment,setIncrement]=useState(1);constincrementRef=useRef(1);functiondecreaseIncrement(){constprevIncrement=increment;constnewIncrement=prevIncrement-1;setIncrement(newIncrement);incrementRef.current=newIncrement;}functionincreaseIncrement(){constprevIncrement=increment;constnewIncrement=prevIncrement+1;setIncrement(newIncrement);incrementRef.current=newIncrement;}useEffect(()=>{constid=setInterval(()=>{constlocalIncrement=incrementRef.current;setCount(c=>c+localIncrement);},1000);return()=>{clearInterval(id);};},[]);return(<><h1>
Counter: {count}<buttononClick={()=>setCount(0)}>Reset</button></h1><hr/><p>
Every second, increment by:
<buttondisabled={increment===0}onClick={()=>{decreaseIncrement()}}>–</button><b>{increment}</b><buttononClick={()=>{increaseIncrement();}}>+</button></p></>);}
AbdiHaryadi
changed the title
[Suggestion]: Make useEffectEvent be usable by default in the challenges or add new section for useRef's alternative
[Suggestion]: Make useEffectEvent be usable by default in the challenges or add a new note for useRef's alternative
Feb 15, 2025
Summary
Make
useEffectEvent
be usable by default in Separating Events from Effects' challenges, or add a new note foruseRef
's alternative.Page
https://react.dev/learn/separating-events-from-effects
Details
I attempted to solve the first challenge in Separating Events from Effects with
useEffectEvent
. First, following the previous example, I imported that function with this line:Then, I used it with these lines:
However, this error was shown up:
Therefore, by default, I can't use
useEffectEvent
for that exercise. I wrote "by default" because maybe it can be used by opening it in CodeSandbox. It looks like an extra effort for me, so I haven't tried it yet.One of the solution I found for this challenge is using
useRef
. I think, the key for this challenge is how to avoid passing reactive elements to the Effect. BecauseuseRef
somehow can be treated as local variable which can be changed outside of render, and the interval itself is in-between render, I think it's safe to use. I need to make a Ref consistent withincrement
state. So, I modified each callback function of the increment buttons.Here is my solution:
It would be better if the
useRef
's alternative is included as a new note in Separating Events from Effects section.The text was updated successfully, but these errors were encountered: