redux-toolkit
redux-toolkit copied to clipboard
Add `pendingMeta` to `createAsyncThunk`'s `thunkAPI`
It is sometimes useful the have the same meta attributes in both the pending and fulfilled actions when using createAsyncThunk.
A solution to this could be to add the result of getPendingMeta to the thunkAPI argument, so the payload creator can use it without needing to recompute it again.
I looked at the source code and it seems easy to use a variable for the getPendingMeta result and add it to thunkAPI above.
Another solution would be to have a way to add a meta attribute to every action (and not only pending).
In our use-case, we are using a middleware to handle displaying a page-wide loading bar when some actions are loading remote data. It would be useful to be able to do something like this:
getMeta: ({ arg: { id } }, { getState }) => ({
showLoadingBar: true,
locked: getState().accounts.get(id)?.locked || false,
}),
The showLoadingBar would be used by the middleware, and the locked attribute is needed for both pending and fulfilled in the reducer.