xxxxxxxxxx
if (typeof localStorage !== 'undefined') {
localStorage.setItem('key', 'value');
} else if (typeof sessionStorage !== 'undefined') {
// Fallback to sessionStorage if localStorage is not supported
sessionStorage.setItem('key', 'value');
} else {
// If neither localStorage nor sessionStorage is supported
console.log('Web Storage is not supported in this environment.');
}
xxxxxxxxxx
if (typeof window !== 'undefined') {
localStorage.setItem('myCat', 'Tom');
}
xxxxxxxxxx
import { useEffect } from "react";
useEffect(() => {
// Perform localStorage action
const item = localStorage.getItem('key')
}, [])