T
TanStack3y ago
extended-salmon

How to fetch useMutation like useQuery on mount (without having to call mutate in useEffect)?

Calling mutate in useEffect makes the call to the BE twice (because of react strict mode) and does two mutations, which is a no go. An option would be to use useQuery intead of useMutation for this mutation, but that's probably a wrong approach.
3 Replies
quickest-silver
quickest-silver3y ago
React only does that in when in development so it won't cause an issue in production. But then again I wouldn't recommend calling either mutation or query in a useEffect if you can help it.
extended-salmon
extended-salmonOP3y ago
I tried this: const hasMounted = useRef(false); useEffect(() => { if (!hasMounted.current) { mutate(selectedPlayers); hasMounted.current = true; } }, []); The mutation happens (the new match is visible in the BE), but it stays in a state of loading all the time for some reason. Any idea why?
foreign-sapphire
foreign-sapphire3y ago
GitHub
Mutations can not be started from an effect in Reacts StrictMode · ...
Describe the bug When firing a mutation from a useEffect on component creation the mutation get's stuck in loading state, despite the underlying request finishing. This only seems to happen in ...

Did you find this page helpful?