Reactjs Audio button?

前端 未结 2 1133
悲&欢浪女
悲&欢浪女 2020-12-22 05:19

In html I create a audio button by this code


        
        

        
2条回答
  •  情书的邮戳
    2020-12-22 05:38

    onClick in react expects a function . But when you do onClick = {this.aud_play_pause()}, it is returned a value . Change the onClick definition to

    onClick = {this.aud_play_pause}
    

    Code:

    var React = require('react');  
    
    var Test = React.createClass( {  
            aud_play_pause() {
                var myAudio = this.mytune;
                if (myAudio.paused) {
                    myAudio.play();
                } else {
                    myAudio.pause();
                }
            },
            render () {
                return(
                   
    ); } }); export default Test;

    You will also need a bundler for instance webpack to transpile your code.

    See the setup example here:

    Also if you use the latest version of React , you will get a warning that afte v15.5.0 React.createClass will be deprecated and hence it will be better to start of with the React.Component

    import React from 'react';  
    
    class Test extends React.Component{
    
            aud_play_pause() {
                var myAudio = this.myTune;
                if (myAudio.paused) {
                    myAudio.play();
                } else {
                    myAudio.pause();
                }
            }
            render () {
                return(
                   
    ); } } export default Test;

提交回复
热议问题