-
Notifications
You must be signed in to change notification settings - Fork 9
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
Proposal implementation of handleActions #23
Comments
I've been trying to get this working, but with a slightly different pattern: I am at a loss, do any of you have any hints? It looks very similar to what you have going on. const SET_TOKENS = 'SET_TOKENS';
const Actions = {
setTokens: (tokens: ApiToken[]) => createAction(SET_TOKENS, tokens),
};
export type Actions = ActionsUnion<typeof Actions>; I am struggling especially with the types for this part.. const handleSetTokens = (state, payload) => ({ ...state, payload });
export const reducers = new Map([[SET_TOKENS, handleSetTokens]]); import reducers from ...
const startReducers = (action$, defaultState = []) =>
action$.pipe(
filter(action => reducers.has(action.type)),
scan(
(state, { type, ...payload }) => reducers.get(type)(state, payload),
defaultState
),
startWith(defaultState)
);
startRoutines(action$); |
@gillchristian This doesn't work for me in Typescript because this line:
|
@raybooysen even with that Check it out here: http://bit.ly/2WAWFZw Try changing the handlers you pass to It'd be great if we could fix this error but I don't think it changes the result. Also, I published what's in the playground as a separate package https://github.com/housinganywhere/safe-redux |
Thanks will give it a try. Checking your code's TS, there is still the error with the handler object. How are you disabling this error? |
@gillchristian Another interesting thing. In a vague roundabout way, I wrote something similar to yours (but I use immer to mutate a draft instead of recreating the state each time). Where I got unstuck was trying to do something like this (using your pattern in the sample):
In the sample above, it remove a large amount of code inside the However, because We could do something like this:
which would give us the type-safety, but introduces another level of function calls. I'm in two minds whether this actually matters so much, other than there just being MORE code. I think this may end up being the best of the options. BTW, none of this is an indictment on your code, I've just been grappling with the same sort of problem trying to ensure that all our reducers pick up the changes to actions. Fun stuff. :) |
I found out about the error when you pointed it out, it's not shown in my editor but it is on the playground. Probably because of a different TS config.
Of course 🎉 I've been using the handlers inline all the time, so I never had the need, but it really makes sense to extract them. Found a way to do it with type Handler<State, ActionType extends string, Actions> =
(s: State, a: ActionsOfType<Actions, ActionType>) => State
const barHandler: Handler<State, 'bar', Actions> =
(s, { payload }) => ({n: s.n + payload})
handleActions<State, Types, Actions>(
{
foo: (s) => ({n: s.n + 1}),
bar: barHandler,
},
{ n: 0},
) Updated the playground: http://bit.ly/2F1V0Gz And I think I will add it to the library when I get some time. EDIT: added |
You absolute hero! I've been battling with my implementation of
The use of |
Really nice solution for typing Redux! It's exactly what I've been looking for for a while! Thanks! 😄
Feature
requestproposalUse case(s)
One thing I really like from
redux-actions
ishandleActions
. For me it's much cleaner thanswitch
statements.The problem is it doesn't play well with
rex-tils
's approach, which is much better IMHO.I created this version of
handleActions
:Which can be used like this:
Enums are not necessary, it works with constants but requires a few lines more:
Props
switch
version, type of action is inferred for each case, but much less boilerplatehandler
object expects all the types to be "handled" (i.e. you get a type error if one of the action types is not covered)Cons
handleActions
expects 3 type parameters. I tried to find a way to make it work only withState
andActions
but I'm not sure is possible. Maybe I'm missing something.If you think this is worth adding to
rex-tils
I can create a Pull Request.The text was updated successfully, but these errors were encountered: