antonioru / beautiful-react-hooks

🔥 A collection of beautiful and (hopefully) useful React hooks to speed-up your components and hooks development 🔥

Home Page:https://antonioru.github.io/beautiful-react-hooks/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

useAudio hook can't change src when input new src

bs32g1038 opened this issue · comments

const audioRef = useRef<HTMLAudioElement>(new Audio(src))

demo:

const UseAudioComponent = () => {
  const [src, setSrc] = useState("");
  const [state, controls] = useAudio(src, { autoPlay: true });
  useEffect(() => {
    setSrc("https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3");
  }, []);
};

expect:
After change src source, audio can play normally.

commented

hi @bs32g1038 thanks for opening this issue, I'm not sure the useAudio hook is supposed to work that way to be honest... if you want it to be reactive please feel free to submit a PR

commented

The way I see this is that it should be done this way:

const UseAudioComponent = () => {
  const [state1, controls1] = useAudio(src1, { autoPlay: true });
  const [state2, controls2] = useAudio(src2, { autoPlay: false });
  
  useEffect(() => {
    controls2.play()
  }, []);
};

I'm closing this for now