Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore the toString override, but keep it out of the docs. #3877

Merged
merged 1 commit into from Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/usage/usage-guide.md
Expand Up @@ -300,7 +300,7 @@ const reducer = createReducer({}, (builder) => {

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"`.

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

```js
const actionCreator = createAction('SOME_ACTION_TYPE')
Expand Down
8 changes: 6 additions & 2 deletions packages/toolkit/src/createAction.ts
Expand Up @@ -224,7 +224,8 @@ export type PayloadActionCreator<
/**
* A utility function to create an action creator for the given action type
* string. The action creator accepts a single argument, which will be included
* in the action object as a field called payload.
* in the action object as a field called payload. The action creator function
* will also have its toString() overridden so that it returns the action type.
*
* @param type The action type to use for created actions.
* @param prepare (optional) a method that takes any number of arguments and returns { payload } or { payload, meta }.
Expand All @@ -239,7 +240,8 @@ export function createAction<P = void, T extends string = string>(
/**
* A utility function to create an action creator for the given action type
* string. The action creator accepts a single argument, which will be included
* in the action object as a field called payload.
* in the action object as a field called payload. The action creator function
* will also have its toString() overridden so that it returns the action type.
*
* @param type The action type to use for created actions.
* @param prepare (optional) a method that takes any number of arguments and returns { payload } or { payload, meta }.
Expand Down Expand Up @@ -273,6 +275,8 @@ export function createAction(type: string, prepareAction?: Function): any {
return { type, payload: args[0] }
}

actionCreator.toString = () => `${type}`

actionCreator.type = type

actionCreator.match = (action: Action<string>): action is PayloadAction =>
Expand Down
7 changes: 7 additions & 0 deletions packages/toolkit/src/tests/createAction.test.ts
Expand Up @@ -9,6 +9,13 @@ describe('createAction', () => {
})
})

describe('when stringifying action', () => {
it('should return the action type', () => {
const actionCreator = createAction('A_TYPE')
expect(`${actionCreator}`).toEqual('A_TYPE')
})
})

describe('when passing a prepareAction method only returning a payload', () => {
it('should use the payload returned from the prepareAction method', () => {
const actionCreator = createAction('A_TYPE', (a: number) => ({
Expand Down