-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add a utility to follow thunk network status #550
Comments
Yeah, loading state is a complex field of thought. Part of the point of I agree that data fetching patterns seem to be the main focus in much of today's development world, and that there is potential value in having more built-in abstractions that simplify that. On the other hand, I don't want RTK to start turning into something like Apollo itself. I refuse to get involved in complex questions like caching, because A) that's out of scope for RTK itself, B) I don't have the knowledge to work on that sort of thing, and C) I have far too many other things on my plate to spend time on that sort of stuff. Given that we've had success porting Specific to the loading state question, David Khourshid has been strongly urging folks to manage that using enums and state machines, and he specifically added an explanatory section to the "treat reducers as state machines" section of the Redux Style Guide showing how that can work. Summarizing all that: I'm not against having some built-in pieces that manage loading state somehow, but I do see it as a potential trap of sorts that could lead to a lot of time spent bikeshedding and trying to cover too many use cases at once. I'm open to suggestions, but it's not something that's a high priority for me right now. |
Ok makes total sense !
My idea is to have something similar to If you want to, I can post more code samples as I explore the idea, and if you have additional guidance or examples from other projects, I'm all ears |
Sure, it's always easier to discuss concrete code ideas :) |
What do you think of creating a separate slice that stores all data fetch states of an application? const requestStatesAdapter = createEntityAdapter<RequestState>({
selectId: (requestState) => requestState.id,
});
const slice = createSlice({
name: 'requestStates',
initialState: requestStatesAdapter.getInitialState(),
reducers: {
setInProgress(state, { payload: { id } }: PayloadAction<WithId<InProgressState>>) {
requestStatesAdapter.upsertOne(state, { isLoading: true, id, error: null });
},
setFailed(state, { payload: { id, error } }: PayloadAction<WithId<FailedState>>) {
requestStatesAdapter.upsertOne(state, { isLoading: false, id, error });
},
setSucceed(state, { payload: { id } }: PayloadAction<WithId>) {
requestStatesAdapter.upsertOne(state, { isLoading: false, id, error: null });
},
clearState: requestStatesAdapter.removeOne,
},
}); It can be used as an event bus for all requests. Errors can be handled in both ways: by custom entity's components and by a universal component of requests errors. But one thing. It requires a component to know what ID it has to use to get the request state. What do you think? Is it a kind of suitable approach? |
Having said that "I don't want to get involved in caching", I am unfortunately starting to rethink that, per #603 . |
The upcoming "RTK Query" API for Redux Toolkit seems to cover this: |
Possibly related issues:
#418
Motivation
Something I've been doing over and over when I'm creating a thunk, is to add a
loading
anderror
properties in the corresponding slice of my store, in a similar fashion as what's described in the tutorial.In the most simple implementations, loading is a boolean, or in more advanced ones an enum like this one from the Apollo library: https://github.com/apollographql/apollo-client/blob/master/src/core/networkStatus.ts
I've found it very useful when using Apollo to have this advanced loading state/ network status provided by default, especially for paginated requests.
Would it fall in the scope for Redux-toolkit to provide a mechanism to create those for a thunk ?
Implementation suggestions
I've done a simple example where I'm creating a reducer given a thunk (returned from
createAsyncThunk
.) Handling of the various loading state, concurrent requests etc. is not complete (fechMore, polling are missing) but all that logic could be implemented here and provide a nice default to properly handle loading state.The text was updated successfully, but these errors were encountered: