Replies: 1 comment
-
This is kinda verbose, but what about something like this: // my-context.js
import { createContext } from 'react'
const MyContext = createContext({ audioContext: null, setAudioContext: () => {} })
export default MyContext
// my-provider.js
function Provider({children}){
const [audioContext, setAudioContext] = React.useState(null)
return <MyContext.Provider value={{audioContext, setAudioContext}}>{children}</MyContext.Provider>
}
export default function StateProvider({children}) {
const ctx = React.useContext(MyContext)
React.useEffect(() => {
ctx.setAudioContext(new AudioContext())
},[])
return <Provider>{children}</Provider>
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm able to instantiate a
new AudioContext()
inside useEffect() but I'm not sure the best approach for exporting a singular AudioContext() object.I'm having difficulty with the syntax for exporting global AudioContext() so that I can useContext() in the function above.
If I move
AudioContext()
intouseEffect()
my audioContext appears to be undefined, I'm pretty sure this is an issue with my syntax as AudioContext doesn't exist at build?Beta Was this translation helpful? Give feedback.
All reactions