Skip to content

Commit

Permalink
Add connect-to-state-with-url-hash.md
Browse files Browse the repository at this point in the history
  * Remove docs from persisting-store-data.md
  • Loading branch information
joel-jo-querypie committed Sep 7, 2022
1 parent 961452c commit 8273c5a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 40 deletions.
48 changes: 48 additions & 0 deletions docs/guides/connect-to-state-with-url-hash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
title: Connect to state with URL hash
nav: 15
---

## State is connected with URL hash

If you want to connect state of a store to URL hash, you can create your own hash storage.

```ts
import create from 'zustand'
import { persist, StateStorage } from 'zustand/middleware'

const hashStorage: StateStorage = {
getItem: (key): string => {
const searchParams = new URLSearchParams(location.hash.slice(1))
const storedValue = searchParams.get(key)
return JSON.parse(storedValue)
},
setItem: (key, newValue): void => {
const searchParams = new URLSearchParams(location.hash.slice(1))
searchParams.set(key, JSON.stringify(newValue))
location.hash = searchParams.toString()
},
removeItem: (key): void => {
const searchParams = new URLSearchParams(location.hash.slice(1))
searchParams.delete(key)
location.hash = searchParams.toString()
},
}

export const useBoundStore = create(
persist(
(set, get) => ({
fishes: 0,
addAFish: () => set({ fishes: get().fishes + 1 }),
}),
{
name: 'food-storage', // unique name
getStorage: () => hashStorage,
}
)
)
```

## CodeSandbox Demo

https://codesandbox.io/s/silly-fire-gsjbc7?file=/src/App.tsx
40 changes: 0 additions & 40 deletions docs/integrations/persisting-store-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -519,46 +519,6 @@ export const useBoundStore = create(
)
```

### How can I connect state to URL hash.

If you want to connect store to URL hash, you can create your own hash storage.

```ts
import create from 'zustand'
import { persist, StateStorage } from 'zustand/middleware'

const hashStorage: StateStorage = {
getItem: (key): string => {
const searchParams = new URLSearchParams(location.hash.slice(1))
const storedValue = searchParams.get(key)
return JSON.parse(storedValue)
},
setItem: (key, newValue): void => {
const searchParams = new URLSearchParams(location.hash.slice(1))
searchParams.set(key, JSON.stringify(newValue))
location.hash = searchParams.toString()
},
removeItem: (key): void => {
const searchParams = new URLSearchParams(location.hash.slice(1))
searchParams.delete(key)
location.hash = searchParams.toString()
},
}

export const useBoundStore = create(
persist(
(set, get) => ({
fishes: 0,
addAFish: () => set({ fishes: get().fishes + 1 }),
}),
{
name: 'food-storage', // unique name
getStorage: () => hashStorage,
}
)
)
```

### How can I rehydrate on storage event?

You can use the `persist` api to create your own implementation, similar to what we see below
Expand Down

0 comments on commit 8273c5a

Please sign in to comment.