Tjorriemorrie
Tjorriemorrie

Reputation: 17282

How to get audio element?

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?

Upvotes: 6

Views: 4989

Answers (1)

Tjorriemorrie
Tjorriemorrie

Reputation: 17282

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"/>
        );
    }
});

Upvotes: 8

Related Questions