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也應該不需要重新綁定的。
React取不到ref的對象
藍山帝景
2018-09-06 09:19:58