2015-07-27 13 views
5

Korzystając z React, chcę uzyskać element audio.Jak uzyskać element audio?

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

ale wciąż otrzymaniu błąd:

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

pewnością obniżonymi składnikami są React zajęcia?

Odpowiedz

6

Działa za pomocą odniesień komponent:

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

React.findDOMNode jest nieaktualna od React 0.14.0, zamiast używać ReactDOM.findDOMNode –