Skip to content

Commit 4be1e9e

Browse files
CodyCodes95dbritto-devdai-shi
authored
fix(docs): errors and types fix for URL state example (#2218)
* 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 <[email protected]> * prettier run --------- Co-authored-by: Danilo Britto <[email protected]> Co-authored-by: Daishi Kato <[email protected]>
1 parent 517524d commit 4be1e9e

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

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

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,18 @@ const persistentStorage: StateStorage = {
7171
if (getUrlSearch()) {
7272
const searchParams = new URLSearchParams(getUrlSearch())
7373
const storedValue = searchParams.get(key)
74-
return JSON.parse(storedValue)
74+
return JSON.parse(storedValue as string)
7575
} else {
7676
// Otherwise, we should load from localstorage or alternative storage
77-
return JSON.parse(localStorage.getItem(key))
77+
return JSON.parse(localStorage.getItem(key) as string)
7878
}
7979
},
8080
setItem: (key, newValue): void => {
8181
// Check if query params exist at all, can remove check if always want to set URL
8282
if (getUrlSearch()) {
8383
const searchParams = new URLSearchParams(getUrlSearch())
8484
searchParams.set(key, JSON.stringify(newValue))
85-
window.history.replaceState(null, null, `?${searchParams.toString()}`)
85+
window.history.replaceState(null, '', `?${searchParams.toString()}`)
8686
}
8787

8888
localStorage.setItem(key, JSON.stringify(newValue))
@@ -94,24 +94,33 @@ const persistentStorage: StateStorage = {
9494
},
9595
}
9696

97-
let localAndUrlStore = (set) => ({
98-
typesOfFish: [],
99-
addTypeOfFish: (fishType) =>
100-
set((state) => ({ typesOfFish: [...state.typesOfFish, fishType] })),
101-
102-
numberOfBears: 0,
103-
setNumberOfBears: (newNumber) =>
104-
set((state) => ({ numberOfBears: newNumber })),
105-
})
97+
type LocalAndUrlStore = {
98+
typesOfFish: string[]
99+
addTypeOfFish: (fishType: string) => void
100+
numberOfBears: number
101+
setNumberOfBears: (newNumber: number) => void
102+
}
106103

107-
let storageOptions = {
104+
const storageOptions = {
108105
name: 'fishAndBearsStore',
109-
storage: persistentStorage,
106+
storage: createJSONStorage<LocalAndUrlStore>(() => persistentStorage),
110107
}
111108

112-
const useLocalAndUrlStore = create(persist(localAndUrlStore, storageOptions))
109+
const useLocalAndUrlStore = create(
110+
persist<LocalAndUrlStore>(
111+
(set) => ({
112+
typesOfFish: [],
113+
addTypeOfFish: (fishType) =>
114+
set((state) => ({ typesOfFish: [...state.typesOfFish, fishType] })),
115+
116+
numberOfBears: 0,
117+
setNumberOfBears: (numberOfBears) => set(() => ({ numberOfBears })),
118+
}),
119+
storageOptions,
120+
),
121+
)
113122

114-
export default localAndUrlStore
123+
export default useLocalAndUrlStore
115124
```
116125

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

0 commit comments

Comments
 (0)