Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

nextjs localstorage

useEffect(() => {
    // setter
    localStorage.setItem('myData', data);
    // getter
    localStorage.getItem('myData');
    // remove
    localStorage.removeItem('myData');
    // remove all
    localStorage.clear();
}, [])
Comment

localStorage nextjs

/*
	Next JS runs your code twice making using window impossible
    So you can do this
*/

window?.localStorage?.whatever()
Comment

nextjs local storage

//local storage hook for SSR
import { useCallback, useEffect, useState } from "react";

export const useLocalStorage = (key, initialValue) => {
  const initialize = (key) => {
    try {
      const item = localStorage.getItem(key);
      if (item && item !== "undefined") {
        return JSON.parse(item);
      }

      localStorage.setItem(key, JSON.stringify(initialValue));
      return initialValue;
    } catch {
      return initialValue;
    }
  };

  const [state, setState] = useState(null)

  useEffect(()=>{
    setState(initialize(key));
  },[]);

  const setValue = useCallback(
    (value) => {
      try {
        const valueToStore = value instanceof Function ? value(storedValue) : value;
        setState(valueToStore);
        localStorage.setItem(key, JSON.stringify(valueToStore));
      } catch (error) {
        console.log(error);
      }
    },
    [key, setState]
  );

  const remove = useCallback(() => {
    try {
      localStorage.removeItem(key);
    } catch {
      console.log(error);
    }
  }, [key]);

  return [state, setValue, remove];
};

// src:
// https://stackoverflow.com/questions/68808837/next-js-use-localstorage-problem-with-ssr
Comment

localstorage in next js

import { FunctionComponent, createElement, useState, MouseEvent, useEffect } from 'react'

interface Props {
    type: string 
}

const CleanupLocalStorage: FunctionComponent<Props> = (props: Props) => {

  const LocalStorage = ({ type }): void => {
    switch (type) {
        case 'clear':
            type !== undefined && window.localStorage.clear()
            break
        default:
            alert('localStorage type undefined')
            break
    }
}

useEffect(() => {
    LocalStorage(props)
},[])

 
    return <></>
}
 
export default CleanupLocalStorage
Comment

PREVIOUS NEXT
Code Example
Javascript :: How to include route handlers in multiple files in Express 
Javascript :: devtool google 
Javascript :: packages.json from file 
Javascript :: symbols javascript 
Javascript :: barcode scanner react js 
Javascript :: react native add react native vector icons not working 
Javascript :: js csv to json 
Javascript :: uselayouteffect 
Javascript :: typescript jsdoc interface 
Javascript :: sliding puzzle javascript 
Javascript :: prop types in react 
Javascript :: react router dom v6 navigate replace 
Javascript :: return two arrays javascript 
Javascript :: if mobile screen in js 
Javascript :: create random password 
Javascript :: return the first matching object from an array 
Javascript :: how to make an object in javascript 
Javascript :: hasChildNodes 
Javascript :: call,bind and apply in javascript 
Javascript :: jquery find element 
Javascript :: reactjs 
Javascript :: resize canvas 
Javascript :: nestjs prisma 
Javascript :: object javascript 
Javascript :: when to use previous state in useState 
Javascript :: regular expression 
Javascript :: button clicker code 
Javascript :: repeat pattern regex 
Javascript :: how to add external link in angular 
Javascript :: json_extract in non native query 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =