I think the promise Returned By getRunningQueryThunk does not sync with actual useQuery life cycle in RTKQ.
const args = { name: "pikachu" };
const Pokemon = () => {
const { data } = useGetPokemonByNameQuery(args);
const dispatch = useDispatch();
const thunk = pokemonApi.util.getRunningQueryThunk("getPokemonByName", args);
const promise = dispatch(thunk as any);
console.log("promise", promise);
if (promise) {
throw promise;
}
return (
<>
<h3>{data.species.name}</h3>
<img src={data.sprites.front_shiny} alt={data.species.name} />
</>
);
};
export default function App() {
return (
<div className="App">
<Suspense fallback={<div>loading...</div>}>
<Pokemon />
</Suspense>
</div>
);
}
I tried using React Suspense with RTKQ with api.util.getRunningQueryThunk method, but no matter what I do, the first value of promise is always undefined. I thought because 'useGetPokemonByNameQuery' was called before 'getRunningQueryThunk', the promise returned by dispatch(thunk) should be a promise which is pending, but it is not.
if I put if (!data) { return <div>loading...</div>}
for making things work, the promise is logged with 3 times with pending phase.
const args = { name: "pikachu" };
const Pokemon = () => {
const { data } = useGetPokemonByNameQuery(args);
const dispatch = useDispatch();
const thunk = pokemonApi.util.getRunningQueryThunk("getPokemonByName", args);
const promise = dispatch(thunk as any);
console.log("promise", promise);
if (promise) {
throw promise;
}
if (!data) {
return <div>loading...</div>;
}
return (
<>
<h3>{data.species.name}</h3>
<img src={data.sprites.front_shiny} alt={data.species.name} />
</>
);
};
export default function App() {
return (
<div className="App">
<Suspense fallback={<div>loading...</div>}>
<Pokemon />
</Suspense>
</div>
);
}
here is my code sandbox link in case you need this.
https://codesandbox.io/s/jolly-rain-gclhfp?file=/src/App.tsx:522-575
I want to know if I am doing something wrong or not.
thank you.