GithubHelp home page GithubHelp logo

bring-shrubbery / use-search-params-state Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 1.79 MB

Use search params as a state in your web application.

Home Page: https://sp-hooks.vercel.app

License: Apache License 2.0

JavaScript 5.44% TypeScript 92.12% CSS 1.43% MDX 1.01%
rsc search-params typescript use-state usestate

use-search-params-state's Introduction

Search Params Hooks

build

Features

  • ๐Ÿ˜Œ Sync state to the URL Search Params and back again.
  • ๐Ÿ’ช Use your own state manager.
  • ๐Ÿค“ Keeps URL clean by automatically removing default values.
  • ๐Ÿ˜ณ React Server Components ready.
  • ๐Ÿš€ Next.js integration.
  • ๐Ÿคฏ Full TypeScript support.
  • ๐Ÿ˜‡ Integrations for SvelteKit/Solid.js coming soon.
  • โšก๏ธ Accepts Zod schema for validation and parsing (WIP).

Packages

Package Latest Version
@sp-hooks/react react-npm
@sp-hooks/next next-npm

Getting Started

Step 1 โญ๏ธ

Before we start, don't forget to star this repo and follow @bring-shrubbery, thanks!

Next.js

pnpm add @sp-hooks/next

React.js

pnpm add @sp-hooks/react

Examples

Simple Next.js usage

Following example will render a button, which when clicked will toggle the button text between "hello" and "world". It will also update the search params to include the value, which means that after refreshing the page, the state will be preserved.

import { useState } from "react";

import { useObserveAndStore } from "@sp-hooks/next";

const Component = () => {
  const [darkMode, setDarkMode] = useState(false);

  useObserveAndStore({ darkMode });

  return (
    <button onClick={() => setDarkMode(!darkMode)}>
      {darkMode ? "๐ŸŒš" : "โ˜€๏ธ"}
    </button>
  );
};

Roadmap

  • Store React state in URL Search Params.
  • Next.js integration.
  • Default values (remove default values from URL).
  • Remove falsy values.
  • Preserve initial keys - keys are preserved in search params, if they were initially set.
  • Array values.
  • Zod validation.
    • Coercing into typed values.
    • Zod default values.
    • Zod optional values.
  • Type-safe state from default values or when validation schema is provided.
  • More validation tools (yup, etc.).
  • Svelte/SvelteKit
  • Solid

Credits

This project is built and maintained by Antoni

If you need help building anything that has a frontend, check out Quassum

License

Apache 2.0 License

use-search-params-state's People

Contributors

bring-shrubbery avatar github-actions[bot] avatar turbobot-temp avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

use-search-params-state's Issues

feat: Array values

Describe the feature you'd like to request

Developers should be able to store multiple values for a single key, without needing to handle array serialisation themselves.

Describe the solution you'd like to see

Developers should be allowed to use string[] as a value type.

Additional information

URLSearchParams API lets you have multiple values for the same key, by using .append and .getAll functions for setting / reading respectively.

feat: Key sorting

Describe the feature you'd like to request

Should be able to sort keys that are saved in the url.

Describe the solution you'd like to see

There should be an option available to enable/disable sorting, with the default set to true, to automatically sort keys by default.

Additional information

No response

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.