This website collects cookies to deliver better user experience
Handling Browser Storage with React Hooks
Handling Browser Storage with React Hooks
This is a very common issue when you need to store data in the browser by using any methods sessionStorage or localStorage with React Hooks.
let's get rid out of it. 😎
Scenario
I have a language that I change on the selection of dropdown and store on the browser.
const[language, setLanguage]=useState(null);constchangeLang=()=>{// update languagesetLanguage("en-IN");// store language in browser as welllocalStorage.setItem('language', language);}
Does the above snippet look fine and store data???🙄 Noooo!!!! It can’t store on the first hit, Because of the async behavior of setLanguage in useState() hooks.