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

Require type to be specified on actions #564

Closed
wants to merge 2 commits into from
Closed

Require type to be specified on actions #564

wants to merge 2 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Aug 18, 2015

Fixes #541

@gaearon
Copy link
Contributor

gaearon commented Sep 4, 2015

@acdlite @johanneslumpe @goatslacker

What do you think? Should we do this?

@goatslacker
Copy link

I like the requirement, the error message is probably confusing though if you want to let the dev know that they are not passing in the correct action.

@ghost
Copy link
Author

ghost commented Sep 7, 2015

@goatslacker Can you tell me what about the message is confusing?
I'll update it to be more clear

@alkhe
Copy link
Contributor

alkhe commented Sep 7, 2015

It could introduce an unnecessary bar in the future when we might develop use cases for Reducers that don't rely on type.

@acdlite
Copy link
Collaborator

acdlite commented Sep 8, 2015

I could go either way on this. The only semi-reasonable situation I can think of where this might get in the way is when using non-plain-object actions, like Immutable.js Maps, in which case action.type would be undefined (but perhaps action.get('type') is not).

@johanneslumpe
Copy link
Contributor

I feel that this would allow us to further standardize the actions we accept and their structure. @acdlite how feasible is using immutable for actions over plain objects? Would it be possible for those people to just tack on a type property into their immutable object? A normal prop, not something that needs to be accessed with get().

This change definitely catches errors where one might destructure the wrong action type from a constants file and else would have a hard time noticing why things break (typos etc.). I' learning towards this.

@alkhe
Copy link
Contributor

alkhe commented Sep 8, 2015

@johanneslumpe Couldn't that be implemented as middleware? If you want to throw errors if the action doesn't have a type, you could write a middleware that does that instead of putting in the core.

type might also be a reserved keyword in some server or compile-to-JS language we haven't yet considered. And in the core, it's only used for '@@INIT' and a debugging function.

It's also already on @acdlite's FSA spec, which a lot of people seem to be using.

@johanneslumpe
Copy link
Contributor

@edge That spec is exactly why I think having it in the core is a good thing - it furthers the standardization of the action shape. It is definitely possible to write a middleware which does the checking, no arguing there. But why force the user to do this type of check on their own, when it can happily live in the core and provide a benefit for all?

In terms of compile-to languages: I'd assume that you can put anything into a hash. If something would prevent you from adding a certain string as a key that would be kind of weird, no?

@gaearon
Copy link
Contributor

gaearon commented Sep 8, 2015

It could introduce an unnecessary bar in the future when we might develop use cases for Reducers that don't rely on type.

Why? Even if reducers don't look at type (e.g. "entities" reducer in "real-world" example folder), the actions still need types for logging and cases when later you might want to add a reducer handling this specific type.

@gaearon
Copy link
Contributor

gaearon commented Sep 8, 2015

The only semi-reasonable situation I can think of where this might get in the way is when using non-plain-object actions, like Immutable.js Maps

We don't allow them anyway. Currently we force actions to be plain objects.

@gaearon
Copy link
Contributor

gaearon commented Sep 8, 2015

Couldn't that be implemented as middleware?

The most common mistake we want to catch is typo in constant name (and thus undefined type). I think we'll catch many more such mistakes if this was included in core instead of being opt-in.

@acdlite
Copy link
Collaborator

acdlite commented Sep 8, 2015

We don't allow them anyway. Currently we force actions to be plain objects.

@gaearon Forgot about that. If we're already making that requirement, then it this additional requirement seems only natural.

@conundrumer
Copy link

@gajus would have complaints, given his convention of using name instead of type. But I see everyone else using type, so...

@gajus
Copy link
Contributor

gajus commented Sep 9, 2015

I agree with @edge that this belongs be in the middleware.

@conundrumer Yes, but there is a way to use Flux Standard Action (i.e. "type" property) with Canonical Reducer Composition, https://github.com/gajus/canonical-reducer-composition#flux-standard-action. redux-convention is used to convert actions from FSA to CCA and vice-versa. In theory, if such a requirement were imposed, you could use the same middleware to workaround it, i.e. convert the action back to FSA at the end of the middleware store.

On the other hard, I don't agree that "type" is the right name for the property to begin with.

@gaearon
Copy link
Contributor

gaearon commented Sep 12, 2015

On the other hard, I don't agree that "type" is the right name for the property to begin with.

Maybe it wasn't, but since this is pretty much a convention at this point, there needs to be a very compelling reason to break everyone by changing the property name. However, some property name needs to be enforced because there is much debugging benefit to preventing “bad constant import” errors early. Therefore we converge on type and enforce it.

@gaearon
Copy link
Contributor

gaearon commented Sep 12, 2015

Rebased and merged as 71393b7 and 87a9b26.

@silvenon
Copy link

What would be the recommended type of API actions in the real-world example then? Since now it throws an error.

@gaearon
Copy link
Contributor

gaearon commented Feb 12, 2016

What error? I will double check but examples have worked fine for a long period of time since this merge. The types are specified by the API middleware that interprets these actions.

@silvenon
Copy link

My bad, my API middleware wasn't plugged in 😓

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

Successfully merging this pull request may close these issues.

8 participants