Created
November 11, 2020 19:04
-
-
Save kellenmace/bac547ef1df4b78c84d01bfa560a2568 to your computer and use it in GitHub Desktop.
Example of using useSound() hook in a React context provider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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