Skip to content

Commit

Permalink
fix(docs): errors and types fix for URL state example (#2218)
Browse files Browse the repository at this point in the history
* Update connect-to-state-with-url-hash.md

Resolved a few errors and type errors in the persist and create state with URL example:

1. createJsonStorage not being called in storageOptions resulting in a type error.
2. Correct hook not being exported
3. Moved the creation of initial state inline to get the correct types passed from create/persist.
4. Used state type as generic for persist.

* yarn prettier run

* Update docs/guides/connect-to-state-with-url-hash.md

Better name for state in setter

Co-authored-by: Danilo Britto <dbritto.dev@gmail.com>

* prettier run

---------

Co-authored-by: Danilo Britto <dbritto.dev@gmail.com>
Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>
  • Loading branch information
3 people committed Dec 31, 2023
1 parent 517524d commit 4be1e9e
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions docs/guides/connect-to-state-with-url-hash.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,18 @@ const persistentStorage: StateStorage = {
if (getUrlSearch()) {
const searchParams = new URLSearchParams(getUrlSearch())
const storedValue = searchParams.get(key)
return JSON.parse(storedValue)
return JSON.parse(storedValue as string)
} else {
// Otherwise, we should load from localstorage or alternative storage
return JSON.parse(localStorage.getItem(key))
return JSON.parse(localStorage.getItem(key) as string)
}
},
setItem: (key, newValue): void => {
// Check if query params exist at all, can remove check if always want to set URL
if (getUrlSearch()) {
const searchParams = new URLSearchParams(getUrlSearch())
searchParams.set(key, JSON.stringify(newValue))
window.history.replaceState(null, null, `?${searchParams.toString()}`)
window.history.replaceState(null, '', `?${searchParams.toString()}`)
}

localStorage.setItem(key, JSON.stringify(newValue))
Expand All @@ -94,24 +94,33 @@ const persistentStorage: StateStorage = {
},
}

let localAndUrlStore = (set) => ({
typesOfFish: [],
addTypeOfFish: (fishType) =>
set((state) => ({ typesOfFish: [...state.typesOfFish, fishType] })),

numberOfBears: 0,
setNumberOfBears: (newNumber) =>
set((state) => ({ numberOfBears: newNumber })),
})
type LocalAndUrlStore = {
typesOfFish: string[]
addTypeOfFish: (fishType: string) => void
numberOfBears: number
setNumberOfBears: (newNumber: number) => void
}

let storageOptions = {
const storageOptions = {
name: 'fishAndBearsStore',
storage: persistentStorage,
storage: createJSONStorage<LocalAndUrlStore>(() => persistentStorage),
}

const useLocalAndUrlStore = create(persist(localAndUrlStore, storageOptions))
const useLocalAndUrlStore = create(
persist<LocalAndUrlStore>(
(set) => ({
typesOfFish: [],
addTypeOfFish: (fishType) =>
set((state) => ({ typesOfFish: [...state.typesOfFish, fishType] })),

numberOfBears: 0,
setNumberOfBears: (numberOfBears) => set(() => ({ numberOfBears })),
}),
storageOptions,
),
)

export default localAndUrlStore
export default useLocalAndUrlStore
```

When generating the URL from a component, you can call buildShareableUrl:
Expand Down

1 comment on commit 4be1e9e

@vercel
Copy link

@vercel vercel bot commented on 4be1e9e Dec 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.