Skip to content

actionCreator#toString didn't work on switch case #72

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

Closed
agusputra opened this issue Jan 7, 2019 · 9 comments
Closed

actionCreator#toString didn't work on switch case #72

agusputra opened this issue Jan 7, 2019 · 9 comments

Comments

@agusputra
Copy link

It seems action creator cannot be used inside switch case. case action1 didn't call toString()

Sample code:

import { createAction } from "redux-starter-kit"

const action1 = createAction("action1")
const theAction = action1()

switch (theAction.type) {
  case action1:
    console.log(theAction.type)
    break
  default:
    console.log('DEFAULT')
}

// Output: DEFAULT
@denisw
Copy link
Contributor

denisw commented Jan 9, 2019

Unfortunately, JavaScript's semantics don't allow customizing how the case matching is done; it's always as if comparing with ===. Your only option is to call toString() directly:

switch (theAction.type) {
  case action1.toString():
    console.log(theAction.type)
    break
  default:
    console.log('DEFAULT')
}

A possible readability improvement could be to let createAction() set not only toString(), but also a type attribute on the action creator function. Then the above could be written as:

switch (theAction.type) {
  case action1.type:
    console.log(theAction.type)
    break
  default:
    console.log('DEFAULT')
}

@denisw
Copy link
Contributor

denisw commented Jan 10, 2019

@markerikson Another benefit of adding a type field to createAction() action creators would be slightly better support for non-string action types - specifically, being able to retrieve them from the action creator in their non-string-converted form.

Arguably, this is an edge case though, and redux-starter-kit is clearly opinionated towards string action types.

@agusputra
Copy link
Author

Unfortunately, JavaScript's semantics don't allow customizing how the case matching is done; it's always as if comparing with ===. Your only option is to call toString() directly:

switch (theAction.type) {
  case action1.toString():
    console.log(theAction.type)
    break
  default:
    console.log('DEFAULT')
}

A possible readability improvement could be to let createAction() set not only toString(), but also a type attribute on the action creator function. Then the above could be written as:

switch (theAction.type) {
  case action1.type:
    console.log(theAction.type)
    break
  default:
    console.log('DEFAULT')
}

Yeah it's because I see the example here: 6da70cb#diff-5f580528e66e02eecffa3be1b7314c3aR31

I think you should update the example or add createAction()#type

@markerikson
Copy link
Collaborator

@agusputra : hmm. yeah, that one may be my fault for writing a bad example.

@neurosnap
Copy link

Another solution:

switch (theAction.type) {
  case `${action1}`:
    console.log(theAction.type)
    break
  default:
    console.log('DEFAULT')
}

@markerikson
Copy link
Collaborator

Action creators now have a .type field, and the docs have been updated. Sorry for the confusion!

markerikson pushed a commit that referenced this issue Apr 20, 2021
* Code Splitting docs 
* Add CSB Example
* more info on code splitting

Co-authored-by: Lenz Weber <[email protected]>
phryneas pushed a commit that referenced this issue Nov 2, 2021
Generates void type when Request QueryArgs are empty.
@zivgit
Copy link

zivgit commented May 4, 2022

动作创建者现在有一个.type字段,并且文档已更新。对困惑感到抱歉!

Seems like there is still a problem with the endpoint ;-(

postApi.endpoints.getPostsByUserId.matchFulfilled.type // undefined
postApi.endpoints.getPostsByUserId.matchFulfilled // [Function anonymous], not [Function actionCreator]

@phryneas
Copy link
Member

phryneas commented May 4, 2022

@zivgit

Those are matcher functions, not action creators. They do not have a .type property.

You have to use the extraReducers builder notation, with addMatcher, not addCase to use those. By the way, we will be deprecating the extraReducers object notation and you should switch away from that in general. (see #2301)

@zivgit
Copy link

zivgit commented May 4, 2022

@phryneas

thank you for the explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants