Skip to content

Commit 16f5d62

Browse files
committed
Restore the toString override, but keep it out of the docs.
1 parent 0dc7fdd commit 16f5d62

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

docs/usage/usage-guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ const reducer = createReducer({}, (builder) => {
300300

301301
This means you don't have to write or use a separate action type variable, or repeat the name and value of an action type like `const SOME_ACTION_TYPE = "SOME_ACTION_TYPE"`.
302302

303-
If you want to use one of these action creators in a switch statement, you need to call `actionCreator.type` yourself:
303+
If you want to use one of these action creators in a switch statement, you need to reference `actionCreator.type` yourself:
304304

305305
```js
306306
const actionCreator = createAction('SOME_ACTION_TYPE')

packages/toolkit/src/createAction.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ export type PayloadActionCreator<
224224
/**
225225
* A utility function to create an action creator for the given action type
226226
* string. The action creator accepts a single argument, which will be included
227-
* in the action object as a field called payload.
227+
* in the action object as a field called payload. The action creator function
228+
* will also have its toString() overridden so that it returns the action type.
228229
*
229230
* @param type The action type to use for created actions.
230231
* @param prepare (optional) a method that takes any number of arguments and returns { payload } or { payload, meta }.
@@ -239,7 +240,8 @@ export function createAction<P = void, T extends string = string>(
239240
/**
240241
* A utility function to create an action creator for the given action type
241242
* string. The action creator accepts a single argument, which will be included
242-
* in the action object as a field called payload.
243+
* in the action object as a field called payload. The action creator function
244+
* will also have its toString() overridden so that it returns the action type.
243245
*
244246
* @param type The action type to use for created actions.
245247
* @param prepare (optional) a method that takes any number of arguments and returns { payload } or { payload, meta }.
@@ -273,6 +275,8 @@ export function createAction(type: string, prepareAction?: Function): any {
273275
return { type, payload: args[0] }
274276
}
275277

278+
actionCreator.toString = () => `${type}`
279+
276280
actionCreator.type = type
277281

278282
actionCreator.match = (action: Action<string>): action is PayloadAction =>

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ describe('createAction', () => {
99
})
1010
})
1111

12+
describe('when stringifying action', () => {
13+
it('should return the action type', () => {
14+
const actionCreator = createAction('A_TYPE')
15+
expect(`${actionCreator}`).toEqual('A_TYPE')
16+
})
17+
})
18+
1219
describe('when passing a prepareAction method only returning a payload', () => {
1320
it('should use the payload returned from the prepareAction method', () => {
1421
const actionCreator = createAction('A_TYPE', (a: number) => ({

0 commit comments

Comments
 (0)