简体   繁体   English

你能帮我处理 JSX 和 React 吗?

[英]Can you help me with JSX and React?

I do a music player on react.js, but i have a problem Its my code:我在 react.js 上做了一个音乐播放器,但我有一个问题我的代码:

const Player = () => {
const audio = new Audio('https://z1.fm//download/17440536')
audio.load();
audio.autoplay = false
return (
    <div className="player-box">
<div className="MainPlayer">
<i className="fa fa-backward  player-btn"/>
<i 
onClick={ () => {
  audio.play()
  } 
} className='fa fa-play player-btn'/>
<i 
onClick={ () => {
  audio.pause()
  } 
} className='fa fa-pause player-btn'/>
<i className="fa fa-forward player-btn" />
<i className="fa fa-random player-btn"/>
<i className="fa fa-refresh player-btn"/>
<input type="range"  className="slider" id="myRange"/> 
</div>
</div>
)

} }

/// I want to do single play/pause button, but when i was doing that, i had many bugs with audio its playing 2 times, but stopping only 1, how i can kill this bug? /// 我想做单次播放/暂停按钮,但是当我这样做时,我有很多音频错误,它播放了 2 次,但只停止了 1 次,我怎样才能杀死这个错误?

If your code represents ReactJS component than you could try something like that:如果您的代码代表 ReactJS 组件,那么您可以尝试以下操作:

class AudioPlayer extends React.Component {
  constructor(props) {
    this.audioPlayer = new Audio(props.initialSource);
    this.audioPlayer.load();
  }

  render() {
    <div className="player-box">
      <div className="MainPlayer">
        <i className="fa fa-backward  player-btn"/>
        <i 
          onClick={this.onTogglePlayClick} 
          className='fa fa-play player-btn'
        />
        <i className="fa fa-forward player-btn" />
        <i className="fa fa-random player-btn"/>
        <i className="fa fa-refresh player-btn"/>
        <input type="range"  className="slider" id="myRange"/> 
      </div>
    </div>
  }

  onTogglePlayClick() {
    const audioPlayer = this.audioPlayer;
    if (audioPlayer.paused) {
      audioPlayer.play();
    } else {
      audioPlayer.pause();
    }
  }
}

Few things I can notice:我能注意到的几件事:

  1. You can use useState hook to keep track of wether the audio is playing or not.您可以使用 useState 挂钩来跟踪音频是否正在播放。

  2. You are not instantiating the audio in every render.您没有在每个渲染中实例化音频。 Whenever you want to introduce side effects to your component you should take advantage of useEffect hook.每当你想为你的组件引入副作用时,你应该利用 useEffect 钩子。 You might want to read https://reactjs.org/docs/hooks-effect.html您可能想阅读https://reactjs.org/docs/hooks-effect.html

you can take a look at that example:你可以看看那个例子:

import React from "react";
import "./styles.css";

export default function App() {
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <Player/>
    </div>
  );
}

const Player = () => {
  const [isPlaying, setIsPlaying] = React.useState(false);
  const [audio, setAudio] = React.useState()


    React.useEffect(() => {
      const audio = new Audio('https://z1.fm//download/17440536')
      audio.load();
      audio.autoplay = false
      setAudio(audio);
    }, [])
  const play = () => {
    setIsPlaying(true);
    audio.play();
  }

  const pause = () => {
    setIsPlaying(false);
    audio.pause();
  }
  return (
      <div className="player-box">
  <div className="MainPlayer">
  <i className="fa fa-backward  player-btn"/>
  {isPlaying ? <span 
  onClick={pause} 
   className='fa fa-pause player-btn'>pause</span> :  <span
  onClick={play} 
   className='fa fa-play player-btn'>play</span>}


  <i className="fa fa-forward player-btn" />
  <i className="fa fa-random player-btn"/>
  <i className="fa fa-refresh player-btn"/>
  <input type="range"  className="slider" id="myRange"/> 
  </div>
  </div>
  )}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM