Skip to content

Commit

Permalink
Merge branch 'main' into v5
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Dec 13, 2023
2 parents ee248eb + 41838c5 commit 5c99468
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
22 changes: 7 additions & 15 deletions docs/guides/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -443,15 +443,9 @@ const bearStore = createStore<BearState>()((set) => ({
}))

function useBearStore(): BearState
function useBearStore<T>(
selector: (state: BearState) => T,
equals?: (a: T, b: T) => boolean,
): T
function useBearStore<T>(
selector?: (state: BearState) => T,
equals?: (a: T, b: T) => boolean,
) {
return useStore(bearStore, selector!, equals)
function useBearStore<T>(selector: (state: BearState) => T): T
function useBearStore<T>(selector?: (state: BearState) => T) {
return useStore(bearStore, selector!)
}
```
Expand All @@ -471,15 +465,13 @@ const bearStore = createStore<BearState>()((set) => ({
increase: (by) => set((state) => ({ bears: state.bears + by })),
}))

const createBoundedUseStore = ((store) => (selector, equals) =>
useStore(store, selector as never, equals)) as <S extends StoreApi<unknown>>(
const createBoundedUseStore = ((store) => (selector) => useStore(store)) as <
S extends StoreApi<unknown>,
>(
store: S,
) => {
(): ExtractState<S>
<T>(
selector: (state: ExtractState<S>) => T,
equals?: (a: T, b: T) => boolean,
): T
<T>(selector: (state: ExtractState<S>) => T): T
}

type ExtractState<S> = S extends { getState: () => infer X } ? X : never
Expand Down
11 changes: 3 additions & 8 deletions docs/integrations/persisting-store-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ export const useBoundStore = create(
)
```

If you're using a type that JSON.stringify() doesn't support, you'll need to write your own serialization/deserialization code. However, if this is tedious, you can use third-party libraries to serialize and deserialize different types of data.
If you're using a type that `JSON.stringify()` doesn't support, you'll need to write your own serialization/deserialization code. However, if this is tedious, you can use third-party libraries to serialize and deserialize different types of data.

For example, [Superjson](https://github.com/blitz-js/superjson) can serialize data along with its type, allowing the data to be parsed back to its original type upon deserialization

Expand Down Expand Up @@ -735,15 +735,10 @@ export const useBearStore = create<MyState>()(

### How do I use it with Map and Set

With the previous persist API, you would use `serialize`/`deserialize`
to deal with `Map` and `Set` and convert them into
an Array so they could be parsed into proper JSON.
In order to persist object types such as `Map` and `Set`, they will need to be converted to JSON-serializable types such as an `Array` which can be done by defining a custom `storage` engine.

The new persist API has deprecated `serialize`/`deserialize`.

Now, you will need to use the `storage` prop.
Let's say your state uses `Map` to handle a list of `transactions`,
then you can convert the Map into an Array in the storage prop:
then you can convert the `Map` into an `Array` in the `storage` prop which is shown below:

```ts

Expand Down

0 comments on commit 5c99468

Please sign in to comment.