如何获得音频元素? [英] How to get audio element?
本文介绍了如何获得音频元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用React,我希望获得音频元素.
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:
错误:永久违反:元素似乎既不是ReactComponent也不是DOMNode(键:0、1、2、3、4)
Error: Invariant Violation: Element appears to be neither ReactComponent nor DOMNode (keys: 0,1,2,3,4)
确定降低的组件是React类吗?
Surely lowered components are React classes?
推荐答案
它使用组件引用进行工作:
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"/>
);
}
});
这篇关于如何获得音频元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文