2015-07-27 16 views
5

Sử dụng React, tôi muốn tải phần tử âm thanh.Làm cách nào để tải phần tử âm thanh?

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

Nhưng tôi tiếp tục nhận được lỗi:

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

thành phần Chắc chắn giảm là Phản ứng lớp học?

Trả lời

6

Nó hoạt động bằng cách sử dụng tài liệu tham khảo phần:

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 bị phản đối từ Phản ứng 0.14.0, sử dụng ReactDOM.findDOMNode thay –

Các vấn đề liên quan