A simple easy to digest key-value based storage module wrapper for Next.js based on cookies that persists data on page reloads
as this package is written in TypeScript documentation is directly available as jsdoc/tsdoc intellisense
- SSR Ready
- Store JSON Out of the box
- Lightweight
- Typed with TypeScript (works with JavaScript too)
npm i next-nookies-persist
import App from "next/app";
import React from "react";
import { NookiesProvider, parseNookies } from "next-nookies-persist";
export default class MyApp extends App {
static async getInitialProps({ Component, ctx }) {
return {
pageProps: {
nookies: parseNookies(ctx), // π
...(Component.getInitialProps
? await Component.getInitialProps(ctx)
: {})
}
};
}
render() {
const { Component, pageProps } = this.props;
return (
<NookiesProvider initialValue={pageProps.nookies}> // π
<Component {...pageProps} />
</NookiesProvider>
);
}
}
import React from "react";
import useStorage from "next-nookies-persist";
const Home = () => {
const { nookies, setNookie, removeNookie } = useStorage();
return (
<div>
<pre>{JSON.stringify(nookies, null, 2)}</pre>
<button onClick={() => setNookie("foo", { bar: "baz" })}>Set</button>
<button onClick={() => removeNookie("foo")}>Clear</button>
</div>
);
};
export default Home;
Contributions/Suggestions are always welcome!
MIT Β© harshzalavadiya