-
Bug descriptionHi, great work with the react component. However, I am running into a problem. When I define the option “peaks” I get an infinite loop with the in the console the message “Warning: Maximum update depth exceeded...”. I did not see an option in the @wavesurfer/react to open an issue. I also saw a similar pull request for a similar issue with a different option. However, I don't see how this is also the case for the peaks option. When specifying the peaks data, the waveform does just generate correctly. But thus with an infinite render loop. Environment
Minimal code snippetimport { useRef } from "react"
import { useWavesurfer } from "@wavesurfer/react"
const About = () => {
const containerRef = useRef(null)
const { wavesurfer, isReady, isPlaying, currentTime } = useWavesurfer({
container: containerRef,
waveColor: "purple",
height: 100,
})
async function loadFile() {
if (wavesurfer) {
const filePath = "resources/test.wav"
const audioBuffer = await window.api.loadMediaFile(filePath)
wavesurfer.loadBlob(new Blob([audioBuffer]))
}
}
const onPlayPause = () => {
wavesurfer && wavesurfer.playPause()
}
return (
<>
<div ref={containerRef} />
<button onClick={onPlayPause}>{isPlaying ? "Pause" : "Play"}</button>
<button onClick={() => loadFile()}>Open file</button>
</>
)
}
export default About Expected resultThe react component loaded without a infinite loop. Obtained resultA infinite loop. Screenshots |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
To add something; i can workaround it by using memo: const peaks = useMemo(() => [peakData.data], [peakData]); // Memoize the peaks array
const { wavesurfer, isReady, isPlaying, currentTime } = useWavesurfer({
container: containerRef,
waveColor: "purple",
height: 100,
}) |
Beta Was this translation helpful? Give feedback.
-
That's not a bug. If you inline objects or arrays w/o memoization, it will trigger a React render. In your case just passing Thank you for the donation btw! 🙏 |
Beta Was this translation helpful? Give feedback.
Yes, normally it shouldn't cause an infinite loop though, so there's some combination of factors at play. But either way it's not good to pass literal objects/arrays/functions as React props. They should be memoized with useMemo/useCallback as per your workaround.