I have a RTK query that takes a few seconds to fetch the data. When isSuccess is true I am then trying to fire a dispatch.
const router = useRouter();
const num = router.query.id;
const { data, error, isLoading, isSuccess } = useModByIdQuery(num);
const dispatch = useDispatch();
{
isSuccess && dispatch(addCurrentMod(data));
}
The problem with this is it fires multiple times (4 to be exact). I tried putting it in a useEffect but as the data takes a few seconds to fetch the dispatch errors out (as it's undefined).
I am not really sure the best way to approach fixing this?
Thanks.