npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

recoil-sync-next

v0.0.6

Published

recoil-sync stores for Next.js

Downloads

3,856

Readme

recoil-sync-next

npm version

Recoil Sync stores for Next.js

Caution

The new router for the app/ directory introduced by Next.js 13 is not yet supported.

Features

Installation

npm install recoil recoil-sync recoil-sync-next
# or
yarn add recoil recoil-sync recoil-sync-next
# or
pnpm add recoil recoil-sync recoil-sync-next

URL Persistence

This provides recoil-sync's URL Persistence functionality interfaced with next/router.

Notes

<RecoilURLSyncJSON> and <RecoilURLSyncTransit> always use shallow routing to update the browser URL. When the URL has changed, they cause re-rendering of themselves. Therefore, you should concider to use React.memo() for Page components that use URL synced atoms ( example ).

<RecoilURLSyncJSONNext>

A version of <RecoilURLSyncJSON> that works with next/router to sync atoms with the browser URL using JSON encoding. This should be a child element of <RecoilRoot>.

function RecoilURLSyncJSONNext(props: {
  storeKey?: string | undefined
  location: LocationOption
  children: ReactNode
}): ReactNode

Props

  • storeKey
  • location
    • Tis prop specifies what part of the URL to sync. See URL Location for more info.
  • children
    • React elements in your component tree.

Example

import { RecoilURLSyncJSONNext } from 'recoil-sync-next'

function MyApp({ Component, pageProps }: AppProps) {
  return (
    <RecoilRoot>
      <RecoilURLSyncJSONNext location={{ part: 'queryParams' }}>
        <Component {...pageProps} />
      </RecoilURLSyncJSONNext>
    </RecoilRoot>
  )
}

<RecoilURLSyncTransitNext>

A version of <RecoilURLSyncTransit> that works with next/router to sync atoms with the browser URL using Transit encoding. This should be a child element of <RecoilRoot>.

function RecoilURLSyncTransitNext(props: {
  storeKey?: string | undefined
  location: LocationOption
  handlers?: ReadonlyArray<TransitHandler<any, any>>
  children: ReactNode
}): ReactNode

Props

  • storeKey
  • location
    • Tis prop specifies what part of the URL to sync. See URL Location for more info.
  • handlers
    • The array of user defined custom encoder/decoder object. See Custom Classes for more info.
  • children
    • React elements in your component tree.

Example

import { RecoilURLSyncTransitNext } from 'recoil-sync-next'

function MyApp({ Component, pageProps }: AppProps) {
  return (
    <RecoilRoot>
      <RecoilURLSyncTransitNext location={{ part: 'queryParams' }}>
        <Component {...pageProps} />
      </RecoilURLSyncTransitNext>
    </RecoilRoot>
  )
}

<RecoilURLSyncNext>

A version of <RecoilURLSync> that works with next/router to sync atoms with the browser URL using your own serialize/deserialize algorithms. This should be a child element of <RecoilRoot>.

function RecoilURLSyncNext(props: {
  storeKey?: string | undefined
  location: LocationOption
  serialize: (data: unknown) => string
  deserialize: (str: string) => unknown
  children: ReactNode
}): ReactNode

Props

  • storeKey
  • location
    • This prop specifies what part of the URL to sync. See URL Location for more info.
  • serialize
  • deserialize
  • children
    • React elements in your component tree.

Example

import { RecoilURLSyncNext } from 'recoil-sync-next'
import qs from 'qs'

function MyApp({ Component, pageProps }: AppProps) {
  return (
    <RecoilRoot>
      <RecoilURLSyncNext
        location={{ part: 'queryParams' }}
        serialize={qs.stringify}
        deserialize={qs.parse}
      >
        <Component {...pageProps} />
      </RecoilURLSyncNext>
    </RecoilRoot>
  )
}

Session Storage Persistence Synced with History

Provides persistence of atoms to session storage synced with the position of the history entry. It will save atoms to session storage when the history entry's position is moved. When the user moves backward/forward on history entries (i.e. 'popstate' event is fired), the atoms saved with that position is restored.

<RecoilHistorySyncJSONNext>

To sync atoms with the position of the history entry using JSON encoding. This should be a child element of <RecoilRoot>.

function RecoilHistorySyncJSONNext(props: {
  storeKey?: string | undefined
  children: ReactNode
}): ReactNode

Props

  • storeKey
  • children
    • React elements in your component tree.

Example

import { RecoilHistorySyncJSONNext } from 'recoil-sync-next'

function MyApp({ Component, pageProps }: AppProps) {
  return (
    <RecoilRoot>
      <RecoilHistorySyncJSONNext>
        <Component {...pageProps} />
      </RecoilHistorySyncJSONNext>
    </RecoilRoot>
  )
}

<RecoilHistorySyncTransitNext>

To sync atoms with the position of the history entry using Transit encoding. This should be a child element of <RecoilRoot>.

function RecoilHistorySyncTransitNext(props: {
  storeKey?: string | undefined
  handlers?: ReadonlyArray<TransitHandler<any, any>>
  children: ReactNode
}): ReactNode

Props

  • storeKey
  • handlers
    • The array of user defined custom encoder/decoder object. See Custom Classes for more info.
  • children
    • React elements in your component tree.

Example

import { RecoilHistorySyncTransitNext } from 'recoil-sync-next'

function MyApp({ Component, pageProps }: AppProps) {
  return (
    <RecoilRoot>
      <RecoilHistorySyncTransitNext>
        <Component {...pageProps} />
      </RecoilHistorySyncTransitNext>
    </RecoilRoot>
  )
}

Utilities

initializableAtom

atom, but initial value can be specified when using it.

Note: This is just a utility and does not depend on either Recoil Sync or Next.js.

function initializableAtom<T extends SerializableParam>(options: {
  key: string
  effects?:
    | ReadonlyArray<AtomEffect<T>>
    | ((param: P) => ReadonlyArray<AtomEffect<T>>)
  dangerouslyAllowMutability?: boolean
}): (initialValue: T) => RecoilState<T>

Type Parameters

  • T
    • The type of the atom value. It must be compared using value-equality and must be serializable.

Parameters

See atom for more info.

Return

A function which takes its initialValue.

Example

import { initializableAtom } from 'recoil-sync-next'

const countState = initializableAtom<number>({
  key: 'count',
})

const MyComponent: React.FC = () => {
  const [count, setCount] = useRecoilState(countState(100)) // count is initialized to 100
  ...
}

initializableAtomFamily

atomFamily, but initial value can be specified individually when using it.

Note: This is just a utility and does not depend on either Recoil Sync or Next.js.

function initializableAtomFamily<
  T extends SerializableParam,
  P extends SerializableParam
>(options: {
  key: string
  effects?:
    | ReadonlyArray<AtomEffect<T>>
    | ((param: P) => ReadonlyArray<AtomEffect<T>>)
  dangerouslyAllowMutability?: boolean
}): (parameter: P, initialValue: T) => RecoilState<T>

Type Parameters

  • T
    • The type of the atom value. It must be compared using value-equality and must be serializable.
  • P
    • The type of the paramter that map to each atom. It must be compared using value-equality and must be serializable.

Parameters

See atomFamily for more info.

Return

A function which takes parameter that map to an atom, and its initialValue.

Example

import { initializableAtomFamily } from 'recoil-sync-next'

const countState = initializableAtomFamily<number, string>({
  key: 'count',
})

const MyComponent: React.FC = () => {
  const [count1, setCount1] = useRecoilState(countState('foo', 0))   // count1 is initialized to 0
  const [count2, setCount2] = useRecoilState(countState('bar', 100)) // count2 is initialized to 100
  ...
}