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 :: event listener 
Javascript :: react native icons 
Javascript :: what is local storage and session storage in javascript 
Javascript :: google places API details JS 
Javascript :: js function arguments 
Javascript :: filter bootstrap 
Javascript :: vue sidebar 
Javascript :: javascript unicode character 
Javascript :: dayjs subtract days 
Javascript :: how to do division in javascript 
Javascript :: return the first element in an array javascript 
Javascript :: setting up react on visual studio code 
Javascript :: remove array from array javascript 
Javascript :: Auto increment in firebase realtime database 
Javascript :: react portal example 
Javascript :: for loop in react native 
Javascript :: spotify player react 
Javascript :: js module pattern 
Javascript :: create file node 
Javascript :: create a node 
Javascript :: vars javascript 
Javascript :: add role command discord.js 
Javascript :: set className with ref react 
Javascript :: javascript array methods cheat sheet 
Javascript :: mongodb aggregate $filter check if exists 
Javascript :: millis javascript 
Javascript :: images node backend server 
Javascript :: difference between w component did update and did mount 
Javascript :: jquery view image in codeigniter 
Javascript :: 188.4 - 93.1 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =