Understanding useRef
Preview
- A simple project demonstrating how the useRef hook works
- Instead of using useState to track every letter change in an input field, we can use the useRef hook
- This example leverages the useRef hook for to manage the state efficiently.
- The playerName ref is created, which provides a reference to the input element.
- By accessing playerName.current.value, the input value is retrieved.
- We then set it to the state (enteredPlayerName) only when needed, minimizing unnecessary state updates.
- This approach optimizes performance, especially in scenarios where constant re-rendering is not required for every keystroke.
How to Run the Project
Open a new terminal
git clone [email protected]:elpah/useRefHookExample.git
cd useRefHookExample
install dependencies
npm i
start proejct by running
```console
npm run dev