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
Problem 2: Performance optimization on adding and removing event handlers.
Solution:
In the course, the event listeners are added and removed every time the components re-render due to a change in the state of the count. We can gain performance by using the useRef hook. It might be a bit of an advanced concept this early in the course. But maybe it's worth it to talk about the performance issue of the current approach, so people following along know that's an issue to avoid in real-world and will be addressed later in the course.
constcountRef=useRef(count);// Handling side-effects in hooks as best practices instead of mutating directly in the function body // Updating countRef.current whenever count changesuseEffect(()=>{countRef.current=count;},[count]);// This useEffect is solely for keeping countRef.current in sync with count
Full Code
import{useEffect,useRef,useState}from"react";import{ResetIcon,PlusIcon,MinusIcon}from"@radix-ui/react-icons";importTitlefrom"./Title";importCountfrom"./Count";importButtonfrom"./Button";importCardfrom"./Card";functionCounter(){const[count,setCount]=useState(0);constcountRef=useRef(count);// Updating countRef.current whenever count changesuseEffect(()=>{countRef.current=count;},[count]);// This useEffect is solely for keeping countRef.current in sync with count// Setting up and tearing down event listenersuseEffect(()=>{consthandleArrowUp=(e)=>{if(e.key==="ArrowUp"){e.preventDefault();setCount(countRef.current+1);}};consthandleArrowDown=(e)=>{if(e.key==="ArrowDown"){e.preventDefault();if(countRef.current===0)return;setCount(countRef.current-1);}};consthandleSpace=(e)=>{if(e.code==="Space"){e.preventDefault();setCount(0);}};window.addEventListener("keydown",handleArrowUp);window.addEventListener("keydown",handleArrowDown);window.addEventListener("keydown",handleSpace);return()=>{window.removeEventListener("keydown",handleArrowUp);window.removeEventListener("keydown",handleArrowDown);window.removeEventListener("keydown",handleSpace);};},[]);// Note: No dependencies here, so this setup and teardown happens only onceconsthandleIncrement=()=>setCount(count+1);consthandleDecrement=()=>setCount(count-1);consthandleReset=()=>setCount(0);return(<Card><Titletitle="fancy counter"/><Countcount={count}/><Buttontheme={count===0 ? "reset-btn-disabled" : "reset-btn"}onClick={handleReset}disabled={count===0 ? true : false}><ResetIconclassName="reset-btn-icon"/></Button><divclassName="button-container"><Buttontheme={count===0 ? "count-btn-disabled" : "count-btn"}onClick={handleDecrement}disabled={count===0 ? true : false}><MinusIconclassName="count-btn-icon"/></Button><Buttontheme={"count-btn"}onClick={handleIncrement}disabled={false}><PlusIconclassName="count-btn-icon"/></Button></div></Card>);}exportdefaultCounter;
PS: I added arrow up and arrow down events for increment and decrement and also used a common pure Button component. I disabled the decrement and reset button when the count reaches zero. Below are the CSS styles I used -
Problem 1: Solving the button focus issue conflicting with the space bar event
Solution:
We can simply use
e.preventDefault()
in theif
conditional block.Problem 2: Performance optimization on adding and removing event handlers.
Solution:
In the course, the event listeners are added and removed every time the components re-render due to a change in the state of the count. We can gain performance by using the
useRef
hook. It might be a bit of an advanced concept this early in the course. But maybe it's worth it to talk about the performance issue of the current approach, so people following along know that's an issue to avoid in real-world and will be addressed later in the course.Full Code
PS: I added arrow up and arrow down events for increment and decrement and also used a common pure Button component. I disabled the decrement and reset button when the count reaches zero. Below are the CSS styles I used -
Button Component
The text was updated successfully, but these errors were encountered: