Export function/method from react use effect / hook 从React的useEffect / hook导出函数/方法

huangapple go评论78阅读模式
英文:

Export function/method from react use effect / hook

问题

我在我的项目中有一个wavesurfer实例。这会动态显示各种音频文件。
我要在一个页面上呈现它,我想要播放和暂停按钮来触发wavesurfer。

我不太明白如何传递这些函数?

它们在代码中有标记。

提前感谢!

英文:

I have this instance of wavesurfer ni one of my projects.
This displays various audio files dynamically.
I'm rendering this on a page where i want play and pause buttons to trigger wave surfer.

I don't quite understand how i can pass these functions?

THey are marked in the code.

THanks in advance!

export default function FiddleDisplay({ audioFileUrl }) {
  const waveformRef = React.useRef();
  React.useEffect(() => {
    if (waveformRef.current) {
      const activeWaveColor = '#f5f5f5';
      const playedWaveColor = '#707070';
      const wavesurfer = WaveSurfer.create({
        container: waveformRef.current,
        barWidth: 5,
        cursorWidth: 2,
        waveColor: activeWaveColor,
        progressColor: playedWaveColor,
        hideScrollbar: true,
        autoCenter: false,
        responsive: true,
        width: 100,
        barHeight: 9,
        height: 350,
        interact: true,
        maxCanvasWidth: 2000,
        fillParent: true
      });
      wavesurfer.load(`${audioFileUrl}`);
      wavesurfer.play(); //USE THIS IN OTHER COMPONENT
      wavesurfer.pause(); //USE THIS IN OTHER COMPONENT
    }
  }, []);
  return (
    <>
      <Waveform ref={waveformRef} />
    </>
  );
}

答案1

得分: 1

以下是翻译好的部分:

你需要使用useState来存储对实例的引用并将相关的绑定方法附加到按钮上

export default function FiddleDisplay({
  audioFileUrl,
}) {
  const waveformRef = React.useRef();
  const [waveSurfer, setWaveSurfer] = React.useState();
  React.useEffect(() => {
    if (waveformRef.current) {
      const activeWaveColor = '#f5f5f5';
      const playedWaveColor = '#707070';
      const wavesurfer = WaveSurfer.create({
        container: waveformRef.current,
        barWidth: 5,
        cursorWidth: 2,
        waveColor: activeWaveColor,
        progressColor: playedWaveColor,
        hideScrollbar: true,
        autoCenter: false,
        responsive: true,
        width: 100,
        barHeight: 9,
        height: 350,
        interact: true,
        maxCanvasWidth: 2000,
        fillParent: true
      });
      wavesurfer.load(`${audioFileUrl}`);
      setWaveSurfer(wavesurfer);
    }
  }, []);

  const play = waveSurfer && waveSurfer.play.bind(waveSurfer);
  const pause = waveSurfer && waveSurfer.pause.bind(waveSurfer);
  return (
    <>
      <Waveform ref={waveformRef} />
      <Button onClick={play}>播放</Button>
      <Button onClick={pause}>暂停</Button>
    </>
  );
}

如果你想在其他地方渲染这些按钮你需要从父函数中传递一个setter并将绑定的方法传递回去
英文:

You'll want to store the reference to the instance with useState, and attach the relevant bound methods to buttons:

export default function FiddleDisplay({
audioFileUrl,
}) {
const waveformRef = React.useRef();
const [waveSurfer, setWaveSurfer] = React.useState();
React.useEffect(() =&gt; {
if (waveformRef.current) {
const activeWaveColor = &#39;#f5f5f5&#39;;
const playedWaveColor = &#39;#707070&#39;;
const wavesurfer = WaveSurfer.create({
container: waveformRef.current,
barWidth: 5,
cursorWidth: 2,
waveColor: activeWaveColor,
progressColor: playedWaveColor,
hideScrollbar: true,
autoCenter: false,
responsive: true,
width: 100,
barHeight: 9,
height: 350,
interact: true,
maxCanvasWidth: 2000,
fillParent: true
});
wavesurfer.load(`${audioFileUrl}`);
setWaveSurfer(wavesurfer);
}
}, []);
const play = waveSurfer &amp;&amp; waveSurfer.play.bind(waveSurfer);
const pause = waveSurfer &amp;&amp; waveSurfer.pause.bind(waveSurfer);
return (
&lt;&gt;
&lt;Waveform ref={waveformRef} /&gt;
&lt;Button onClick={play}&gt;Play&lt;/Button&gt;
&lt;Button onClick={pause}&gt;Pause&lt;/Button&gt;
&lt;/&gt;
);
}

If you want to render the buttons somewhere else, you'll need to pass in a setter from a parent function and pass the bound methods back up.

huangapple
  • 本文由 发表于 2020年1月6日 23:00:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614332.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定