Accessing LocalStorage in NextJS Ibrahim Adeniyi on October 05, 2020 I recently migrated a Content Management System from Create React App to NextJS in order to score some SEO points. One of the challenges I faced w... Read full post Submit Preview Dismiss Collapse Expand Jaime López Jaime López Jaime López Follow Experienced Microsoft 365 pro and Microsoft MVP. Skilled in pre-sales, team management, and global projects. Passionate about innovative solutions. Newsletter author. Location Spain Work Digital Workplace Architect (EMEA) — IT Consultant Joined Jul 2, 2019 • Jan 17 '21 Dropdown menu Copy link Hide Nice trick, but I think it would make more sense to use useEffect method, did you try it? useEffect(function() { console.log(window.localStorate); },[]); Enter fullscreen mode Exit fullscreen mode Collapse Expand Simple Neeraj Simple Neeraj Simple Neeraj Follow Full stack web developer, love with typescript, learning app development , interested in startups⚡. Location India, Uttarpardesh, phalauda Education No CS degree Joined Jan 20, 2022 • Jan 20 '22 Dropdown menu Copy link Hide useEffect(function() { console.log(window.localstorage); },[]); Enter fullscreen mode Exit fullscreen mode Because some time we need to copy and paste code, and after code doesn't work after pasting so developers 😯😯😯😯😯. Your Code have some mistakes check out this 👍 Collapse Expand Justus Okwuchukwu Ali Justus Okwuchukwu Ali Justus Okwuchukwu Ali Follow Joined Jun 25, 2021 • Oct 25 '21 Dropdown menu Copy link Hide You may find yourself in a situation where you need access to localStorage outside a react component. In that case useEffect cannot help. Collapse Expand Magnus Bondesson Magnus Bondesson Magnus Bondesson Follow Web/app fullstack dev with focus on frontend Location Stockholm, Sweden Work App dev at BautaBit AB Joined Jan 26, 2021 • Apr 23 '21 Dropdown menu Copy link Hide window.localStorage (spelling) Collapse Expand Simple Neeraj Simple Neeraj Simple Neeraj Follow Full stack web developer, love with typescript, learning app development , interested in startups⚡. Location India, Uttarpardesh, phalauda Education No CS degree Joined Jan 20, 2022 • Jan 20 '22 • Edited on Jan 20 • Edited Dropdown menu Copy link Hide Trying to making a reusable function This function for get data from localstorage key is required const getFromStorage = (key) => { if(typeof window !== 'undefined'){ window.localstorage.getItem(key) } } Enter fullscreen mode Exit fullscreen mode Set data to localstorage const setToStorage = (key,value) => { if(typeof window !== 'undefined'){ return window.localstorage.setItem(key,value) } } Enter fullscreen mode Exit fullscreen mode Any new method to use window in nextjs Collapse Expand Vu Vu Vu Follow I'm a Frontend developer Education Saigontech Joined Dec 10, 2021 • Mar 31 '22 Dropdown menu Copy link Hide That's nice Collapse Expand Ciro Santilli Ciro Santilli Ciro Santilli Follow https://cirosantilli.com Joined Apr 22, 2019 • Jun 25 '21 Dropdown menu Copy link Hide This leads to a warning of type "Expected server HTML to contain a matching" on Next 10 BTW: stackoverflow.com/questions/464436... Collapse Expand ARiyou Jahan ARiyou Jahan ARiyou Jahan Follow Time is Precious. Waste it Wisely! Looking for satisfaction after overcoming programming challenges! In dire need of time. Joined Dec 2, 2022 • Dec 2 '22 • Edited on Dec 2 • Edited Dropdown menu Copy link Hide remember, never create a function for window type checking. it always must be an explicit type checking or it will not work Right way to do it: if (typeof window !== "undefined") { // client-side operation such as local storage. localStorage.setItem(key, value) } Enter fullscreen mode Exit fullscreen mode Wrong way: const hasWindow = () => { return typeof window !== "undefined" } if (hasWindow()) { // client-side operation such as local storage. localStorage.setItem(key, value) } if (!hasWindow()) { // server-side code } Enter fullscreen mode Exit fullscreen mode read more and reason for this problem: Next.js GitHub issues page Collapse Expand Atom Atom Atom Follow I'm Atom, a full-stack developer and UI/UX designer. Location New Jersey Joined Jul 31, 2022 • Sep 17 '22 Dropdown menu Copy link Hide That's nice, but next time add a js tag to the code block to make it more readable. Example: if (typeof window !== "undefined") { localStorage.setItem(key, value) } Enter fullscreen mode Exit fullscreen mode Collapse Expand Ibrahim Adeniyi Ibrahim Adeniyi Ibrahim Adeniyi Follow A senior web developer with over 6 years of experience building things people love Email adeniyiib360@gmail.com Joined Jun 3, 2019 • Jan 2 '23 Dropdown menu Copy link Hide Thank you Collapse Expand Othman Othman Othman Follow Front-end developer love to work with react js Location Cairo Work front-end-developer at student Joined Aug 22, 2020 • Sep 3 '21 Dropdown menu Copy link Hide what if i want to access local storage inside redux reducer ? Collapse Expand 77pintu 77pintu 77pintu Follow Joined Apr 5, 2020 • Apr 1 '23 Dropdown menu Copy link Hide Thanks for the great post !!!!!! Collapse Expand Julien Prugne Julien Prugne Julien Prugne Follow Bored bearded dev for hire.... Location Brest, penn ar bed Work Code enjoyer Joined Feb 23, 2017 • Dec 20 '21 Dropdown menu Copy link Hide Thanks mate! Works the same with sessionStorage Code of Conduct • Report abuse
Nice trick, but I think it would make more sense to use
useEffectmethod, did you try it?Because some time we need to copy and paste code, and after code doesn't work after pasting so developers 😯😯😯😯😯.
Your Code have some mistakes check out this 👍
You may find yourself in a situation where you need access to localStorage outside a react component. In that case useEffect cannot help.
window.localStorage (spelling)
Trying to making a reusable function
This function for get data from localstorage
key is required
Set data to localstorage
Any new method to use window in nextjs
That's nice
This leads to a warning of type "Expected server HTML to contain a matching" on Next 10 BTW: stackoverflow.com/questions/464436...
remember, never create a function for window type checking.
it always must be an explicit type checking or it will not work
Right way to do it:
Wrong way:
read more and reason for this problem: Next.js GitHub issues page
That's nice, but next time add a
jstag to the code block to make it more readable.Example:
Thank you
what if i want to access local storage inside redux reducer ?
Thanks for the great post !!!!!!
Thanks mate!
Works the same with sessionStorage