@location-state/next

1.0.1 • Public • Published

@location-state/next

npm version License: MIT

State management library for React that synchronizes with history location supporting Next.js Pages Router.

Features

  • Manage the state to synchronize with the history location.
  • By default, supports Session Storage and URL as persistent destinations.

Packages

Quickstart for Next.js Pages Router

Installation

npm install @location-state/core @location-state/next
# or
yarn add @location-state/core @location-state/next
# or
pnpm add @location-state/core @location-state/next

Configuration

// src/pages/_app.tsx
import { LocationStateProvider } from "@location-state/core";
import { useNextPagesSyncer } from "@location-state/next";
import type { AppProps } from "next/app";

export default function MyApp({ Component, pageProps }: AppProps) {
  const syncer = useNextPagesSyncer();
  return (
    <LocationStateProvider syncer={syncer}>
      <Component {...pageProps} />
    </LocationStateProvider>
  );
}

Working with state

import { useLocationState } from "@location-state/core";

export function Counter() {
  const [counter, setCounter] = useLocationState({
    name: "counter",
    defaultValue: 0,
    storeName: "session",
  });

  return (
    <div>
      <p>
        storeName: <b>{storeName}</b>, counter: <b>{counter}</b>
      </p>
      <button onClick={() => setCounter(counter + 1)}>increment</button>
    </div>
  );
}

API

View the API reference here.

Readme

Keywords

none

Package Sidebar

Install

npm i @location-state/next

Weekly Downloads

1

Version

1.0.1

License

none

Unpacked Size

9.75 kB

Total Files

10

Last publish

Collaborators

  • koichik
  • akfm