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 send message to user in socket.io 
Javascript :: nested ternary operator javascript 
Javascript :: bootstrap 4 form validator with jquery 
Javascript :: javascript arrays 
Javascript :: acer swift 5 
Javascript :: array.findindex is not a function 
Javascript :: Highest and Lowest 
Javascript :: regex to valied password strength stackoverflow 
Javascript :: jquery default value 
Javascript :: e editable select no button 
Javascript :: cannon js parent child 
Javascript :: reducer function redux 
Javascript :: search filter with react native on flatlist 
Javascript :: moment min 
Javascript :: webpack test js or jsx 
Javascript :: competitive programming in javascript 
Javascript :: angular two way binding 
Javascript :: node.js express export routes 
Javascript :: javascript add to undefined 
Javascript :: How to scan a folder for documents with javascript 
Javascript :: how to download react table into pdf full 
Javascript :: filter the falsy values out of an array in a very simple way! 
Javascript :: how to give index in query selector in js 
Javascript :: for each append to document 
Javascript :: in in sequelize 
Javascript :: Jquery check if hover over child element 
Javascript :: some js es6 
Javascript :: Material-ui cold icon 
Javascript :: how to remove letters from an array javascript 
Javascript :: how to change currency in react-paypal-button-v2 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =