React useRef Hook & localStorage for Persistent Data Storage

Today I learned about the useRef Hook in React and how to use localStorage to manage data in the browser. ** useRef Hook The useRef hook is used to create a reference that persists across renders. It is commonly used to directly access DOM elements or store values without causing re-renders. Example: import { useRef } from "react"; function InputFocus() { const inputRef = useRef(null); const handleClick = () => { inputRef.current.focus(); }; return ( <> <input ref={inputRef} /> <button onClick={handleClick}>Focus Input</button> </> ); } ** localStorage in JavaScript localStorage allows us to store data in the browser so that it remains even after refreshing the page. • setItem() – Store data • getItem() – Retrieve data • update – Modify existing data • removeItem() – Delete data Example: localStorage.setItem("name", "Avesh"); const data = localStorage.getItem("name"); localStorage.removeItem("name"); @Sheryians Coding School @Sarthak Sharma @Daneshwar Verma @Devendra Dhote @Ritik Rajput #ReactJS #JavaScript #FullStackDeveloper #WebDevelopment #CodingJourney

To view or add a comment, sign in

Explore content categories