Skip to content

Instantly share code, notes, and snippets.

@kellenmace
Created November 11, 2020 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kellenmace/bac547ef1df4b78c84d01bfa560a2568 to your computer and use it in GitHub Desktop.
Save kellenmace/bac547ef1df4b78c84d01bfa560a2568 to your computer and use it in GitHub Desktop.
Example of using useSound() hook in a React context provider
import React, { createContext, useContext } from "react"
import useSound from "use-sound"
import popDownSound from "../sounds/pop-down.mp3"
import marioCoinSound from "../sounds/mario-coin.mp3"
import applauseSound from "../sounds/applause.mp3"
import fanfareSound from "../sounds/fanfare.mp3"
const SoundsContext = createContext()
export function SoundsProvider({ children }) {
const [playPopDownSound] = useSound(popDownSound, { volume: 0.25 })
const [playMarioCoinSound] = useSound(marioCoinSound, { volume: 0.25 })
const [playApplauseSound] = useSound(applauseSound, { volume: 0.25 })
const [playFanfareSound] = useSound(fanfareSound, { volume: 0.25 })
const value = {
playPopDownSound,
playMarioCoinSound,
playApplauseSound,
playFanfareSound,
}
return (
<SoundsContext.Provider value={value}>{children}</SoundsContext.Provider>
)
}
const useSounds = () => useContext(SoundsContext)
export default useSounds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment