Localstorage for next js
npm install --save next-persist-state
import React from "react";
import usePersistState from "next-persist-state";
const App = () => {
/** add unique key so that specific state value can be retrieved */
const [value, setValue, clearValue] = usePersistState(
"SAMPLE_KEY",
"Hello World!"
);
return (
<div>
<div>{value}</div>
<div>
<button onClick={() => setValue("New Value")}>set value</button>
<button onClick={() => clearValue()}>clear value</button>
</div>
</div>
);
};
export default App;
MIT © khantwaikyaw
This hook is created using create-react-hook.