Skip to content

Commit

Permalink
docs(middleware): modify both set and setState (#2306)
Browse files Browse the repository at this point in the history
* docs(middleware): modify `set` and `setState`

* update middleware docs
  • Loading branch information
charkour committed Feb 15, 2024
1 parent b466a75 commit eb8443f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 26 deletions.
6 changes: 5 additions & 1 deletion docs/guides/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,11 @@ const loggerImpl: LoggerImpl = (f, name) => (set, get, store) => {
set(...a)
console.log(...(name ? [`${name}:`] : []), get())
}
store.setState = loggedSet
const setState = store.setState
store.setState = (...a) => {
setState(...a)
console.log(...(name ? [`${name}:`] : []), store.getState())
}

return f(loggedSet, get, store)
}
Expand Down
25 changes: 0 additions & 25 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,31 +284,6 @@ clearForest()
[Alternatively, there are some other solutions.](./docs/guides/updating-state.md#with-immer)
## Middleware
You can functionally compose your store any way you like.
```jsx
// Log every time state is changed
const log = (config) => (set, get, api) =>
config(
(...args) => {
console.log(' applying', args)
set(...args)
console.log(' new state', get())
},
get,
api,
)

const useBeeStore = create(
log((set) => ({
bees: false,
setBees: (input) => set({ bees: input }),
})),
)
```
## Persist middleware
You can persist your store's data using any kind of storage.
Expand Down

0 comments on commit eb8443f

Please sign in to comment.