diff --git a/readme.md b/readme.md index db9bfbf32e..69880c2b14 100644 --- a/readme.md +++ b/readme.md @@ -412,6 +412,18 @@ const createBearSlice = (set, get) => ({ }) ``` +You can also log action's type along with its payload: + +```jsx +const createBearSlice = (set, get) => ({ + addFishes: (count) => + set((prev) => ({ fishes: prev.fishes + count }), false, { + type: 'bear/addFishes', + count, + }), +}) +``` + If an action type is not provided, it is defaulted to "anonymous". You can customize this default value by providing an `anonymousActionType` parameter: ```jsx diff --git a/src/middleware/devtools.ts b/src/middleware/devtools.ts index e97c7b5e0a..af72f07d11 100644 --- a/src/middleware/devtools.ts +++ b/src/middleware/devtools.ts @@ -40,11 +40,11 @@ type TakeTwo = T extends [] type WithDevtools = Write, StoreDevtools> type StoreDevtools = S extends { - setState: (...a: infer A) => infer Sr + setState: (...a: infer Sa) => infer Sr } ? { - setState( - ...a: [...a: TakeTwo, actionType?: string | { type: unknown }] + setState( + ...a: [...a: TakeTwo, action?: A] ): Sr } : never diff --git a/tests/devtools.test.tsx b/tests/devtools.test.tsx index 1b396123d4..7b8f47ab74 100644 --- a/tests/devtools.test.tsx +++ b/tests/devtools.test.tsx @@ -80,18 +80,29 @@ describe('If there is no extension installed...', () => { }) describe('When state changes...', () => { - it("sends { type: setStateName || 'anonymous` } as the action with current state", () => { + it("sends { type: setStateName || 'anonymous`, ...rest } as the action with current state", () => { const api = create( devtools(() => ({ count: 0, foo: 'bar' }), { name: 'testOptionsName', enabled: true, }) ) + api.setState({ count: 10 }, false, 'testSetStateName') expect(extension.send).toHaveBeenLastCalledWith( { type: 'testSetStateName' }, { count: 10, foo: 'bar' } ) + + api.setState({ count: 15 }, false, { + type: 'testSetStateName', + payload: 15, + }) + expect(extension.send).toHaveBeenLastCalledWith( + { type: 'testSetStateName', payload: 15 }, + { count: 15, foo: 'bar' } + ) + api.setState({ count: 5, foo: 'baz' }, true) expect(extension.send).toHaveBeenLastCalledWith( { type: 'anonymous' }, diff --git a/tests/middlewareTypes.test.tsx b/tests/middlewareTypes.test.tsx index ae88bae82f..9e51d90b1d 100644 --- a/tests/middlewareTypes.test.tsx +++ b/tests/middlewareTypes.test.tsx @@ -259,18 +259,24 @@ describe('counter state spec (double middleware)', () => { __DEV__ = savedDEV }) - it('devtools & immer', () => { + it('immer & devtools', () => { __DEV__ = false const useBoundStore = create()( - devtools( - immer((set, get) => ({ - count: 0, - inc: () => - set((state) => { - state.count = get().count + 1 - }), - })), - { name: 'prefix' } + immer( + devtools( + (set, get) => ({ + count: 0, + inc: () => + set( + (state) => { + state.count = get().count + 1 + }, + false, + { type: 'inc', by: 1 } + ), + }), + { name: 'prefix' } + ) ) ) const TestComponent = () => {