Skip to content

Commit fc53a5a

Browse files
author
ben.durrant
committed
infer action name from type
1 parent 6189cc3 commit fc53a5a

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

packages/toolkit/src/actionCreatorInvariantMiddleware.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ export interface ActionCreatorInvariantMiddlewareOptions {
1010
}
1111

1212
export function getMessage(type?: unknown) {
13+
const splitType = type ? `${type}`.split('/') : []
14+
const actionName = splitType[splitType.length - 1] || 'actionCreator'
1315
return `Detected an action creator with type "${
1416
type || 'unknown'
1517
}" being dispatched.
16-
Make sure you're calling the action creator before dispatching, i.e. \`dispatch(actionCreator())\` instead of \`dispatch(actionCreator)\`. This is necessary even if the action has no payload.`
18+
Make sure you're calling the action creator before dispatching, i.e. \`dispatch(${actionName}())\` instead of \`dispatch(${actionName})\`. This is necessary even if the action has no payload.`
1719
}
1820

1921
export function createActionCreatorInvariantMiddleware(

packages/toolkit/src/tests/actionCreatorInvariantMiddleware.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('createActionCreatorInvariantMiddleware', () => {
1414
consoleSpy.mockRestore()
1515
})
1616

17-
const dummyAction = createAction('anAction')
17+
const dummyAction = createAction('aSlice/anAction')
1818

1919
it('sends the action through the middleware chain', () => {
2020
const next: Dispatch = (action) => ({

0 commit comments

Comments
 (0)