Reduce boilerplate for RTK Query loading and error states
isLoading and isError states for multiple queries within a component. It allows developers to isolate data-loading logic from presentational components, increasing type certainty by reducing the need for optional chaining and allowing components to be written as if the data is already present. This provides a 'loader' pattern similar to those found in server-side rendering frameworks (like Remix or Next.js) but for Single Page Applications (SPAs).// Traditional approach without RTK Query Loader:
const Component = () => {
const pokemonQuery = useGetPokemon("charizard");
const userQuery = useGetCurrentUser();
const userStatsQuery = useGetUserStats();
if (
pokemonQuery.isLoading ||
userQuery.isLoading ||
userStatsQuery.isLoading
) {
return <LoadingView />;
}
if (
pokemonQuery.isError ||
userQuery.isError ||
userStatsQuery.isError
) {
return (
<ErrorView
error={
pokemonQuery.error ??
userQuery.error ??
userStatsQuery.error
}
/>
);
}
return (
<ComponentWithData
pokemon={pokemonQuery.data}
user={userQuery.data}
userStats={userStatsQuery.data}
/>
);
};