Replies: 7 comments 35 replies
-
@devuxer how did you end up solving this? I've recently run into this issue myself. I'm still new to the react ecosystem, and for various reasons my click handler needs to be wrapped in a // custom react-query hooks as per recommendation
function usePlay() {
const { user } = useUser();
const mutation = useMutation((options) => spotifyApi.play(options));
return user ? mutation : stub;
}
function usePause() {
const { user } = useUser();
const mutation = useMutation(() => spotifyApi.pause());
return user ? mutation : stub;
}
// consuming custom hooks
const pause = usePause();
const play = usePlay();
const playPauseHandler = useCallback(() => {
if (status.state === "playing") {
pause.mutate();
} else {
play.mutate({
uris: [`spotify:track:${item.id}`],
position_ms: progress,
});
}
}, [item.id, status.state, progress]); `` // how can i properly include mutate here? I'm still trying to wrap my head around hooks and react-query. Is it ok to omit the From my limited understanding I could somehow properly include |
Beta Was this translation helpful? Give feedback.
-
putting The
I don't really see why for this use-case, you need to make a request from the frontend:
I would just do:
|
Beta Was this translation helpful? Give feedback.
-
Any other solution for this? 👎🏻 |
Beta Was this translation helpful? Give feedback.
-
useEffect seems like the right place, as triggering the mutation is an imperative action, which can be called at any time - on the useEffect is a particular time. You just need to decide if that is the time you want |
Beta Was this translation helpful? Give feedback.
-
A little bit dirty (since a state is created when not needed) but does the job const mutation = useMutation(...);
useState(mutation.mutate); // run only once per component |
Beta Was this translation helpful? Give feedback.
-
Tony Polinelli
…On Wed, 20 -aaaa1-Mar 2024 at 8:07 pm, Yair ***@***.***> wrote:
1. const mutation = useMutation(...);useState(mutation.mutate); // run only once per component
—
Reply to this email directly, view it on GitHub
<#986 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAXRUNLUX3HXWRYMGO65JCLYZFNXNAVCNFSM4RAPKD62U5DIOJSWCZC7NNSXTOKENFZWG5LTONUW63SDN5WW2ZLOOQ5TQOBVGA4DEMI>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
In my case, it's that I want to fire a mutation because the user clicked on a link in a transactional email to, say, reset their password, and I want to fire a mutation due the action of the user clicking that link the email. Since I can't tie into that user action from the email client, I tie it to the page loading in the browser, than I display the appropriate message in the browser (success, link expired, or whatever). |
Beta Was this translation helpful? Give feedback.
-
I want to have a mutation run when a page loads. Would you recommend putting a
mutate()
call inside auseEffect
in this case or is there a better way?My use case is a user clicking a link in a transactional email to verify their email account.
Beta Was this translation helpful? Give feedback.
All reactions