亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

React取不到ref的對象

React取不到ref的對象

藍山帝景 2018-09-06 09:19:58
class music extends React.Component {constructor(props){  super(props);  this.state = {      isPlay : false   } } componentDidMount(){  console.log('DidMount');  console.log(this.ref.musicBox); } render(){  return (  <div>      <div className="record">       <img src="./images/唱片.svg"/>       </div>       <div className='music'>       <audio ref="musicBox" src="./music/Lost Stars.mp3" controls="controls" loop='loop'>        Your browser does not support the audio element.      </audio>       </div>   </div>  )  } }想要取到audio標簽然后判斷它是否播放。但是在componentDidMount(){}中console它時報錯:DidMount Cannot read property 'musicBox' of undefined其實昨天在另外一個JS里面也遇到這個問題了~但是因為另外一個ref是寫在動態生成的標簽里面的,百度到說動態生成的標簽ref會取不到。但是這里是為什么會取不到ref呢?就在一個組件內,componentDidMount的this也應該不需要重新綁定的。
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 2090 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號