Change callback name returned from react hook

1.4k Views Asked by At

I want to use stop function in useSound Hooks in diffrent name because I alredy used stop variable.

Use Sound Doc

import play from "./assets/play.mp3";

const [Phone,{stopPhone as stop }] = useSound(phone);
2

There are 2 best solutions below

0
BANO notIT On

You should use Object desctructoring like so:

const [Phone, { stop: stopPhone }] = useSound(phone);
2
Hassan Raza On

If useSound returns an array, there should be no need to "rename" any of its items - simply put in the names you want into the destructuring syntax:

const [Phone,stopPhone] = useSound(phone);

you can simply choose whatever names you wish.

If it returns an object you can use object destructuring as follows

const [Phone,{ stop: stopPhone }] = useSound(phone);