How to get audio element?

↘锁芯ラ 提交于 2019-12-21 04:51:38

问题


Using React, I wish to get the audio element.

var AudioPlayer = React.createClass({
    componentDidMount: function () {
        console.info('Audio: component did mount');
        var audio = React.findDOMNode('audio');
        console.info('audio', audio);
    },
    render : function() {
        return (
            <audio src="/static/music/foo.mp3" controls />
        );
    }
});

But I keep receiving the error:

Error: Invariant Violation: Element appears to be neither ReactComponent nor DOMNode (keys: 0,1,2,3,4)

Surely lowered components are React classes?


回答1:


It works using the component references:

var AudioPlayer = React.createClass({
    componentDidMount: function () {
        console.info('[AudioPlayer] componentDidMount...');
        this.props.el = React.findDOMNode(this.refs.audio_tag);
        console.info('audio prop set', this.props.el);
    },
    render: function() {
        console.info('[AudioPlayer] render...');
        return (
            <audio ref="audio_tag" src="/static/music/foo.mp3" controls autoplay="true"/>
        );
    }
});


来源:https://stackoverflow.com/questions/31657335/how-to-get-audio-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!