From dfc590f78c7d5cfe56580f20858b0d982f0f809d Mon Sep 17 00:00:00 2001 From: Charles Kornoelje <33156025+charkour@users.noreply.github.com> Date: Sun, 21 Jan 2024 06:34:13 -0800 Subject: [PATCH 1/2] docs(middleware): modify `set` and `setState` --- docs/guides/typescript.md | 5 ++++- readme.md | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/docs/guides/typescript.md b/docs/guides/typescript.md index 1f9432f4b1..d7fdf105b0 100644 --- a/docs/guides/typescript.md +++ b/docs/guides/typescript.md @@ -259,7 +259,10 @@ const loggerImpl: LoggerImpl = (f, name) => (set, get, store) => { set(...a) console.log(...(name ? [`${name}:`] : []), get()) } - store.setState = loggedSet + store.setState = (...a) => { + store.setState(...a) + console.log(...(name ? [`${name}:`] : []), store.getState()) + } return f(loggedSet, get, store) } diff --git a/readme.md b/readme.md index fd94573108..954c1f097e 100644 --- a/readme.md +++ b/readme.md @@ -291,6 +291,11 @@ You can functionally compose your store any way you like. ```jsx // Log every time state is changed const log = (config) => (set, get, api) => + api.setState = (...args) => { + console.log(' applying', args) + api.setState(...args) + console.log(' new state', get()) + } config( (...args) => { console.log(' applying', args) From fca6276e6a712651c1ec86cb7babb3b74105efca Mon Sep 17 00:00:00 2001 From: Charles Kornoelje <33156025+charkour@users.noreply.github.com> Date: Tue, 13 Feb 2024 08:08:44 -0500 Subject: [PATCH 2/2] update middleware docs --- docs/guides/typescript.md | 3 ++- readme.md | 30 ------------------------------ 2 files changed, 2 insertions(+), 31 deletions(-) diff --git a/docs/guides/typescript.md b/docs/guides/typescript.md index d7fdf105b0..f52708eccb 100644 --- a/docs/guides/typescript.md +++ b/docs/guides/typescript.md @@ -259,8 +259,9 @@ const loggerImpl: LoggerImpl = (f, name) => (set, get, store) => { set(...a) console.log(...(name ? [`${name}:`] : []), get()) } + const setState = store.setState store.setState = (...a) => { - store.setState(...a) + setState(...a) console.log(...(name ? [`${name}:`] : []), store.getState()) } diff --git a/readme.md b/readme.md index 954c1f097e..3ee0ee3558 100644 --- a/readme.md +++ b/readme.md @@ -284,36 +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) => - api.setState = (...args) => { - console.log(' applying', args) - api.setState(...args) - console.log(' new state', get()) - } - 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.