Issue
Hi I am declaring an audio context like this and it says window in undefined. I tried declaring declare const window :any above window.Context but the error's still there. Anyone know how I can solve this?
window.AudioContext = window.AudioContext || (window as any).webkitAudioContext
export default function TestPage(){
const audioContext = new AudioContext();
return <>Hello</>
}
Solution
next.js
runs server side. window
is only available on client side. So you will have to wait until component is mounted, like this:
export default function TestPage(){
const audioContext = useState(null);
useEffect(() => {
window.AudioContext = window.AudioContext || (window as any).webkitAudioContext;
audioContext.current = new AudioContext();
},[]);
return <>Hello</>
}
Answered By - Tushar Shahi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.