Skip to content

Commit

Permalink
docs: update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
asmyshlyaev177 committed Jun 30, 2024
1 parent fa0d7b9 commit 7f49001
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 18 deletions.
82 changes: 64 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,56 @@
[![For hire](/hireBadge.svg)](https://www.linkedin.com/in/asmyshlyaev177/)

# urlstate
<h1 align="center">state-in-url</h1>

<p align="center">
<img src="https://github.com/asmyshlyaev177/urlstate/blob/main/assets/logo.svg?raw=true" alt="Library logo"/>
<img src="/assets/logo.svg?raw=true" alt="Library logo"/>
</p>

<div align="center">
Seamlessly sync React state with URL query parameters in Next.js/React.js applications. Simplify state management, enhance shareability, and maintain type safety—all through your browser's URL.

<hr />


[![For hire](/assets/hireBadge.svg)](https://www.linkedin.com/in/asmyshlyaev177/)


[![npm](https://img.shields.io/npm/v/state-in-url.svg)](https://www.npmjs.com/package/state-in-url)
[![Tests](https://github.com/asmyshlyaev177/state-in-url/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/asmyshlyaev177/state-in-url/actions/workflows/tests.yml)
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](https://github.com/asmyshlyaev177/state-in-url/)
![npm bundle size (minified + gzip)](https://img.shields.io/bundlephobia/minzip/state-in-url.svg)

</div>

# Features
- **Seamless state synchronization with URL query parameters.**
- **Preserves shape and types of data**
- **Full TypeScript support for enhanced developer experience.**
- **Automatic serialization and deserialization of complex objects.**
- **Separate hooks for Next.js (useUrlState) and React.js (useUrlEncode) usage.**
- **Efficient updates with minimal re-renders.**
- **Lightweight, no dependencies.**

## Table of content
- [Installation](#installation)
- [Usage with Next.js](#useurlstate-hook-for-nextjs)
- [Usage with React.js](#useurlencode-hook-for-reactjs)
- [Low-level encode/decode functions](#encode-and-decode-helpers)
- [Gothas](#gothas)
- [Contact & Support](#contact--support)
- [Changelog](#changelog)
- [License](#license)

## installation
```sh
# npm
npm install --save prism-react-renderer
# yarn
yarn add prism-react-renderer
# pnpm
pnpm add prism-react-renderer
```

[![npm](https://img.shields.io/npm/v/urlstate.svg)](https://www.npmjs.com/package/urlstate)
![npm bundle size (minified + gzip)](https://img.shields.io/bundlephobia/minzip/urlstate.svg)

When working with Next.js client components sometimes you need to pass values between them, using `useSearchParams` is a good solution, but this library can help you to serialize complex objects and will preserve types. It is more convinient and types-safe solution for state management.

## useUrlState hook
## useUrlState hook for Next.js

`useUrlState` is a custom React hook for Next.js applications that manages state in the URL query string. It allows you to store and retrieve state from the URL search parameters, providing a way to persist state across page reloads and share application state via URLs.

Expand All @@ -23,7 +61,7 @@ When working with Next.js client components sometimes you need to pass values be

```typescript
'use client'
import { useUrlState } from 'urlstate';
import { useUrlState } from 'state-in-url';

// State shape should be stored in a constant, don't pass an object directly
const countState = { count: 0 };
Expand All @@ -37,6 +75,7 @@ function MyComponent() {
return (
<div>
<p>Count: {state.count}</p>

<button onClick={() => updateUrl({ count: state.count + 1 })}>
Increment (Update URL)
</button>
Expand All @@ -56,7 +95,7 @@ function MyComponent() {

```typescript
'use client'
import { useUrlState } from 'urlstate';
import { useUrlState } from 'state-in-url';

interface UserSettings {
theme: 'light' | 'dark';
Expand Down Expand Up @@ -109,7 +148,7 @@ function Component() {

```typescript
'use client'
import { useUrlState } from 'urlstate';
import { useUrlState } from 'state-in-url';

const someObj = {};

Expand All @@ -118,14 +157,14 @@ function SettingsComponent() {
}
```

## useUrlEncode hook
## useUrlEncode hook for React.js

`useUrlEncode` is a custom React hook that provides utility functions for encoding and decoding state to and from URL search parameters. This hook doesn't depend on Nextjs, and will works with any React application.

Accepts optional defaultState argument.
Accepts optional `defaultState` argument.

```typescript
import { useUrlEncode } from 'urlstate';
import { useUrlEncode } from 'state-in-url';

const Component = () => {
const { parse, stringify } = useUrlEncode();
Expand All @@ -152,9 +191,9 @@ const Component = () => {
There low level helpers to stringify and parse query string params. Useful for other frameworks or pure JS.

```javascript
import { encode, decode } from 'urlstate';
import { encode, decode } from 'state-in-url';

const state = encode({ obj: [1, 2, 3] });
const state = { obj: [1, 2, 3], obj2: true }

// to params
const params = new URLSearchParams();
Expand All @@ -176,5 +215,12 @@ const obj = Object.fromEntries(
1. Can pass only serializable values, `Function`, `BigInt` or `Symbol` won't work, probably things like `ArrayBuffer` neither. But won't throw an error if you do it accidently.
2. Developed and tested with recent `Next.js` and `Typescript` versions.

## Contact & Support

- Create a [GitHub issue](https://github.com/asmyshlyaev177/state-in-url/issues) for bug reports, feature requests, or questions
- Add a ⭐️ [star on GitHub](https://github.com/asmyshlyaev177/state-in-url) to support the project!

## [Changelog](https://github.com/asmyshlyaev177/state-in-url/blob/main/CHANGELOG.md)

## [Changelog](https://github.com/asmyshlyaev177/urlstate/blob/main/CHANGELOG.md)
## License
This project is licensed under the [MIT license](https://github.com/asmyshlyaev177/state-in-url/blob/main/LICENSE).
1 change: 1 addition & 0 deletions assets/hireBadge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7f49001

Please sign in to comment.