From eb8443faa333e5804b372b211424eec05e200b47 Mon Sep 17 00:00:00 2001 From: Charles Kornoelje <33156025+charkour@users.noreply.github.com> Date: Thu, 15 Feb 2024 08:21:29 -0500 Subject: [PATCH] docs(middleware): modify both `set` and `setState` (#2306) * docs(middleware): modify `set` and `setState` * update middleware docs --- docs/guides/typescript.md | 6 +++++- readme.md | 25 ------------------------- 2 files changed, 5 insertions(+), 26 deletions(-) diff --git a/docs/guides/typescript.md b/docs/guides/typescript.md index 1f9432f4b1..f52708eccb 100644 --- a/docs/guides/typescript.md +++ b/docs/guides/typescript.md @@ -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) } diff --git a/readme.md b/readme.md index fd94573108..3ee0ee3558 100644 --- a/readme.md +++ b/readme.md @@ -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.