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 make nextjs image component responsive 
Javascript :: jquery iframe use from js style 
Javascript :: assign key and value to object 
Javascript :: jquery form validation plugin callback function 
Javascript :: revert back to css 
Javascript :: Error: Requires Babel "^7.0.0-0", but was loaded with "6.26.3". 
Javascript :: string split javascript 
Javascript :: js invert color 
Javascript :: jquery get id 
Javascript :: how to define args using param discord.js 
Javascript :: toast in react native 
Javascript :: props type 
Javascript :: conditional object spread 
Javascript :: discord bot playing game 
Javascript :: nestjs change httpcode inside function 
Javascript :: how to get the max value of two variables in math 
Javascript :: javascript alert on refresh 
Javascript :: jquery input value change event not working 
Javascript :: how to get object keys and values in javascript 
Javascript :: multiple value selected in select2 
Javascript :: javascript check if is image 
Javascript :: match password and confirm password in javascript if true then submit form 
Javascript :: javascript enumerate with index 
Javascript :: internal/modules/cjs/loader.js:1122 return process.dlopen(module, path.toNamespacedPath(filename)); 
Javascript :: how to move an element of an array in javascript 
Javascript :: javascript expressions 
Javascript :: check if is function javascript 
Javascript :: bootstrap icons react 
Javascript :: get n number of elements from array javascript 
Javascript :: drupal 8 check if current page is node 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =