1.1.0 ā€¢ Published 10 days ago

react-use-url-state v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 days ago

react-use-url-state

npm npm type definitions npm npm bundle size Known Vulnerabilities

Use URL params to store a state in React.

  • Handle numbers, dates, booleans, and arrays.
  • Have a type-safety with Zod
  • CJS and ESM support
  • 0 dependencies

https://example.com/?text=query&number=10&date=2023-11-29T13%3A52%3A50.230Z

<=>

{ text: 'query', number: 10, date: new Date('2023-11-29T13:52:50.230Z') }

Installation

npm i react-use-url-state zod

Documentation

šŸ“Documentation and examples

Usage

šŸ’¬ It's important to use .coerce on your Zod schema to ensure that the values are parsed correctly from the URL.

function MyComponent() {
  const { data, setState, setValue, setValues, isError, error } = useUrlState(
    z.object({
      name: z.string(),
      age: z.coerce.number(),
      birthDate: z.coerce.date().optional(),
    }),
  );

  return <div>
    <Button
      onClick={() => {
        setState({ name: 'test', age: 10, birthDate: new Date() });
      }}
    >
      Set state
    </Button>

    <Button
      onClick={() => {
        setValue('age', data.age + 1);
      }}
    >
      Increment age
    </Button>

    <pre>{JSON.stringify(data, null, 2)}</pre>
  </div>
}
1.1.0

10 days ago

1.0.1

4 months ago

1.0.0

5 months ago

0.3.2

5 months ago

0.3.1

5 months ago

0.3.0

5 months ago

0.2.0

5 months ago

0.1.1

6 months ago

0.1.0

6 months ago

0.0.5

6 months ago

0.0.4

6 months ago

0.0.3

6 months ago

0.0.2

6 months ago

0.0.1

6 months ago