joshwcomeau / use-sound

A React Hook for playing sound effects
MIT License
2.76k stars 98 forks source link

Help, how to use with React + Typescript #66

Closed webdiego closed 3 years ago

webdiego commented 3 years ago

Hello,

I would love to use this amazing tool, but I'm not able to use with React+Typescript (I've just started to learn them). Tried multiple times, but I've encountered with 2 main error.

  1. I'm not able to import from my folder the mp3 as I get this TS error : Cannot find module or its corresponding type declarations.ts(2307)

  2. type 'PlayFunction' is not assignable to type 'MouseEventHandler'

I hope someone can help me

Thanks! :) @joshwcomeau

trevv16 commented 3 years ago

I am also having this issue, would love a demo file with typescript.

webdiego commented 3 years ago

Yes, exactly. I've just built a website for a Records and I would like to add some cool sound effect! Hope someone can help us :)

trevv16 commented 3 years ago

For now I am able to get by with a defined click handler function like this:

  const clickHandler = () => {
    console.log('test');           //Just to verify its working
    play();
  };

 onClick={clickHandler}.        //on button or div
trevv16 commented 3 years ago

I used a declaration file I named audio.d.ts in the root of my src folder like this:

declare module '*.mp3';
declare module '*.wav';

Tbh I am new to typescript so I am not sure if these are the best practices but the audio plays.

webdiego commented 3 years ago

Nice! Me as well, I've just started to learn it. I'm going to try right now! If you want we can open a project and try to figure out together! ;)

trevv16 commented 3 years ago

Im @trevva_ on twitter hit me up

Ovodo commented 2 years ago

same here this worked for me too. Can someone pls help with an explanation or another method to go about it.Thanks