javascript - React取不到ref的对象
天蓬老师
天蓬老师 2017-04-11 13:20:03
[JavaScript讨论组]
class music extends React.Component {
constructor(props){
  super(props);
  this.state = {
      isPlay : false
  }
}

componentDidMount(){
  console.log('DidMount');
  console.log(this.ref.musicBox);
}

render(){
  return (
  

) } }

想要取到audio标签然后判断它是否播放。但是在componentDidMount(){}中console它时报错:

DidMount
Cannot read property 'musicBox' of undefined

其实昨天在另外一个JS里面也遇到这个问题了~但是因为另外一个ref是写在动态生成的标签里面的,百度到说动态生成的标签ref会取不到。
但是这里是为什么会取不到ref呢?就在一个组件内,componentDidMount的this也应该不需要重新绑定的。

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(1)
巴扎黑

https://facebook.github.io/re...

我看官方用的是this.refs 不是this.ref
你试试看吧 我这里没有运行环境

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号