use-reducer-async
use-reducer-async copied to clipboard
proposal: async callback
Idea is to provide ability to listen for async actions callback. basically is to listen on .then of returned Promise. redux-thunk have that ability, so proposal is to introduce it into that package.
Hi! Thanks for the proposal. I would like to discuss something about this.
- Are you familiar with TypeScript? We need to fix that too.
- Because useReducer's dispatch returns nothing, I'm a bit skeptical of this change being not compatible.
- What is your use case? I would like to learn it, because I would have used a state value instead to know the async callback result.
Meanwhile, let me work on updating master (to eliminate dist
in repo.)
- a bit, what exaclty should be fixed?
- i'm looking on "thunk" variations of redux hooks, and those folks are fine with it - https://github.com/nathanbuchar/react-hook-thunk-reducer/blob/master/src/thunk-reducer.js#L43 . dunno if it's bad or good.
- oh, indeed, i also would like to use state, but in my case it will be super-overkill. in my case there are several AJAX calls, once i receive data i have to a) update state (thats fine) b) trigger some stuff manually on
Thanks for the notes. You could trigger some stuff in async functions, couldn't you. Anyway, I understand your mental model coming from redux-thunk.
Basically, because this change is not breaking anything, I would like to merge it if we were able to solve the type issue. One of the goal of this lib is to provide strict typing.
The issue is React.Dispatch
returns void
. So, we can't simply do Dispatch<ExportAction>
. We somehow need to infer Dispatch type from asyncActionHandlers.
thanks! great to hear that! talking about TS, ya, it's tricky or to verbose to do it in proper manner, as far as i can see. at the same time, will something like that do the trick:
const aaHandler = (
(type && aaHandlers[type]) || null
) as (typeof action extends AsyncAction ? (s: {
dispatch: Dispatch<ReducerAction<R>>;
getState: () => ReducerState<R>;
signal: AbortSignal;
}) => (a: typeof action) => **Promise<Promise<any> | void>** : null);
what do you feel? as far as i understand, it's already returns Promise, so difference is that it can be Promise chain with some output, or void in general case
Well, my goal for this library is to provide proper types in TS. (I mean, this is a tiny lib one could create in JS, pretty easily.)
So, no, we'd like to provide no-any types. Like if your action handler returns Promise<boolean>
, you will get it with dispatch
.
I know it's going to be tricky and verbose. I'd be willing to help coding for this, and hope you would be helping with testing.
What I think is good is to create a failing test either in __tests__
or examples
.
closing as stale.