Skip to content

Instantly share code, notes, and snippets.

@h-jennings
Created January 19, 2023 17:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h-jennings/1a9e2d1d366da2f85e833ebe8f3411f2 to your computer and use it in GitHub Desktop.
Save h-jennings/1a9e2d1d366da2f85e833ebe8f3411f2 to your computer and use it in GitHub Desktop.
const AudioPrimitiveContext = React.createContext<InterpreterFrom<
typeof audioMachine
> | null>(null);
export const AudioPrimitiveProvider = ({
children,
}: {
children?: React.ReactNode;
}) => {
const [machine] = React.useState(() => {
return audioMachine;
});
const audioService = useInterpret(machine);
return (
<AudioPrimitiveContext.Provider value={audioService}>
{children}
</AudioPrimitiveContext.Provider>
);
};
export const useAudioService = () => {
const context = React.useContext(AudioPrimitiveContext);
if (!context) {
throw new Error('useAudioState must be used within a AudioProvider');
}
return context;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment