Very nice explanation and example, tailored to both people not using Redux yet, or thinking about migrating to RTK :) Thanks for this article.
I have a complementary question though: how does RTK integrate with async middlewares such as Thunk? Nowadays, most React websites using Redux for state management also have async actions (API calls, mostly), which come with their own cumbersome stash of boilerplate (creating a pending/success/error action type every time, huge switch blocks in reducers, repeated testing for very similar actions, etc.)
I'm seriously thinking about migrating to RTK, but I don't quite see how this would integrate with our complex mix of async/non-async actions.
That docs page is effectively also the migration guide. Switch your store setup, then migrate one slice reducer at a time.
It also comes with a new "RTK Query" API that completely abstracts the entire data fetching and caching process, and can remove the need to write any thunks or caching reducers whatsoever if you choose to use it:
Very nice explanation and example, tailored to both people not using Redux yet, or thinking about migrating to RTK :) Thanks for this article.
I have a complementary question though: how does RTK integrate with async middlewares such as Thunk? Nowadays, most React websites using Redux for state management also have async actions (API calls, mostly), which come with their own cumbersome stash of boilerplate (creating a pending/success/error action type every time, huge switch blocks in reducers, repeated testing for very similar actions, etc.)
I'm seriously thinking about migrating to RTK, but I don't quite see how this would integrate with our complex mix of async/non-async actions.
RTK already enables the thunk middleware by default:
and includes a
createAsyncThunk
utility that abstracts the typical "dispatch actions before and after the request" pattern:That docs page is effectively also the migration guide. Switch your store setup, then migrate one slice reducer at a time.
It also comes with a new "RTK Query" API that completely abstracts the entire data fetching and caching process, and can remove the need to write any thunks or caching reducers whatsoever if you choose to use it:
Thank you for all these details, I will check it out :D