Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(middleware): modify both set and setState #2306

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@dai-shi, is this what you were suggesting?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, let's see how others think. @dbritto-dev @sewera

Copy link
Collaborator

Choose a reason for hiding this comment

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

yeah, I'm down with you @dai-shi

Copy link
Member

Choose a reason for hiding this comment

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

We might want dedicated middleware guide, but it should be fine for now. Another issue is typescript.md is rather too big.


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
Loading