Open sregg opened 5 years ago
Could you give an example where you need it, just so I am sure I fully understand the use case?
For instance if I have a list of items, I'd like to differentiate the initial API call (loading indicator centered) from the pull-to-refresh (loading indicator at the top) so I'm using a flag isPullToRefresh
in my metadata. Then based on that metadata I'm setting my state isLoading
or isPullRefreshing
.
switch (action.type) {
case String(actionLoadResidentBalances.pending):
const isPullRefreshing = action.meta?.isPullRefreshing ?? false;
return {
...state,
isLoadingCurrentBalances: !isPullRefreshing,
isRefreshingCurrentBalances: isPullRefreshing,
};
To elaborate - in my experimentation, the meta data is present on the async actions. It just isn't present on the type inferred for each of the async actions.
If you cast the type back to the original action type, you can access the meta
property and the data is present as expected, e.g.
export const asyncAction = createAsyncAction(
'myAction',
() => Promise.resolve('a payload'),
() => ({ someValue: 'something'})
);
export const reducer = createReducer(initialState, handleAction => [
handleAction(asyncAction.pending as typeof asyncAction, (state, action) => {
return {
...state,
someProperty: action.meta.someValue
}),
]);
First of all, congrats for the new
createReducer
. This is super helpful as we don't have to define types anymore!The only issue I'm seeing is that I can't seem to grab the
metadata
, only thepayload
infulfilled
state