为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 1996 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号