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

geschichte

v12.4.4

Published

manage query parameters with react-hooks

Downloads

1,750

Readme

📖 Geschichte (/ɡəˈʃɪçtə/)

🇺🇦 Support Ukraine 🇺🇦 Help Provide Humanitarian Aid to Ukraine.

CircleCI Codecov npm semantic-release

Geschichte (german for History / Story / Tale) Let's you manage query-parameters with hooks. Uses immer and zustand to manage the internal state.

Documentation & Demo: https://bowlingx.github.io/geschichte/index.html

API: https://bowlingx.github.io/geschichte/api/index.html

yarn add geschichte

npm install geschichte

Basic Example

import { pm, factoryParameters, serializers } from 'geschichte'
import { GeschichteWithHistory } from 'geschichte/historyjs'
import { createBrowserHistory } from 'history'

const parameterConfig = {
  item: pm(
    'queryParameter',
    serializers.string /** a basic collection of serializers is availble, like date, int, float, arrays */,
    (value?: V, initialValue?: V) =>
      boolean /** define an optional skip function which will determine if the parameter will be included in the url or not */
  ),
  /* ... more keys, any depth. */
}

// default value is either an object or a factory () => defaultValue
const defaultValue = {
  item: 'defaultValue' /** it automatically skips null or default values*/,
}

// exports a hook (`useQuery`), and
// utility methods `createQueryString` that let's you create a query string based on the described object anywhere outside of components etc.
// `parseQueryString` let's you parse a query string into an object as defined in the `parameterConfig`.
const { useQuery, createQueryString, parseQueryString } = factoryParameters(
  parameterConfig,
  defaultValue /** optional namespace, (creates a prefix separated by a dot)*/
)

const Component = () => {
  const {
    values,
    pushState,
    replaceState,
    resetPush,
    resetReplace,
    createQueryString,
    batchReplaceState,
    batchPushState,
  } = useQuery()
  return (
    <>
      <button
        onClick={() => pushState((values) => void (values.item = 'newValue'))}
      >
        push new state
      </button>
      <button
        onClick={() =>
          replaceState((values) => void (values.item = 'anotherOne'))
        }
      >
        replace state
      </button>
      <button onClick={resetPush}>reset (push) to defaults</button>
      <button onClick={resetReplace}>reset (replace) to defaults</button>
      <div>{JSON.stringify(values)}</div>
      <div>The current queryString: {createQueryString()}</div>
    </>
  )
}

const App = () => (
  <GeschichteWithHistory history={createBrowserHistory()}>
    <Component />
  </GeschichteWithHistory>
)

Concept

Geschichte let's you describe and serialize an arbitrary object of any depth to your browsers query and history. It takes care of updating the next state and current query in a efficient way using immerjs. It works on both the browser and server side (with createMemoryHistory)

Naming

I was inspired by immer and zustand, so I picked a fitting german name :).

Agenda

  • Add more tests
  • Propper examples and documentation of the full API
  • Describe Use-Cases

Compability

It works out of the box with react-router (by providing the same history instance).

Using with next.js

Nextjs support is build in, but requires a different Adapter.

With page router

/** _app.tsx */

import React, { memo } from 'react'
import GeschichteForNextjs from 'geschichte/nextjs'
import type { AppProps } from 'next/app'

function App({ Component, pageProps }: AppProps) {
  return (
    <GeschichteForNextjs>
      <Component {...pageProps} />
    </GeschichteForNextjs>
  )
}

export default memo(App)

With App router

You can use Geschichte with the app router as well (from nextjs 13).

/** page.tsx */

import Geschichte from 'geschichte/nextjs-app-router'
import MoreComponentsWithClientSideState from '@/components/ClientComponents'

export default function Home() {
  return (
    <main>
      <header>My header</header>
      <Geschichte>
        <MoreComponentsWithClientSideState />
      </Geschichte>
      <footer>My footer</footer>
    </main>
  )
}