Reactjs 在arrow函数中调用函数是';行不通
我试图调用箭头函数中的函数,但不知怎么的,它不起作用。我做错了什么 这不起作用:Reactjs 在arrow函数中调用函数是';行不通,reactjs,react-native,Reactjs,React Native,我试图调用箭头函数中的函数,但不知怎么的,它不起作用。我做错了什么 这不起作用: restartVideo(ref){ ref.player.seek(0) } onProgress={() => {this.state.restart ? this.restartVideo : null}} 这是: onProgress={() => {this.state.restart ? ref.player.seek(0) : null}} 整个组成部分: <Video so
restartVideo(ref){
ref.player.seek(0)
}
onProgress={() => {this.state.restart ? this.restartVideo : null}}
这是:
onProgress={() => {this.state.restart ? ref.player.seek(0) : null}}
整个组成部分:
<Video source={video} // Can be a URL or a local file.
ref={ref => this.player = ref} // Store reference
rate={1.0} // 0 is paused, 1 is normal.
paused={this.state.paused} // Pauses playback entirely.
onLoadStart={this.loadStart} // Callback when video starts to load
onProgress={() => {this.state.restart ? this.restartVideo : null}} // Callback every ~250ms with currentTime
onEnd={this.backToQuestion} // Callback when playback finishes
onError={this.videoError} // Callback when video cannot be loaded
style={styles.video} />
this.player=ref}//存储引用
速率={1.0}//0暂停,1正常。
暂停={this.state.paused}//完全暂停播放。
onLoadStart={this.loadStart}//视频开始加载时的回调
onProgress={()=>{this.state.restart?this.restartVideo:null}}//使用currentTime每隔250ms进行一次回调
onEnd={this.backToQuestion}//播放结束时回调
OneError={this.videoError}//无法加载视频时回调
style={style.video}/>
事件aconsole.log()
未在restartVideo()内激发
onProgress={() => {this.state.restart ? this.restartVideo : null}}
到
i、 e.指定一个调用this.restartVideo的函数,而不是返回该函数的函数
顺便说一句,看起来您也应该传递ref
(或者通过函数中的this
访问它)。更改
onProgress={() => {this.state.restart ? this.restartVideo : null}}
到
i、 e.指定一个调用this.restartVideo的函数,而不是返回该函数的函数
顺便说一句,看起来您也应该传递ref
(或通过函数中的this
访问它)。如果您在这一行中插入括号:
onProgress={()=>{this.state.restart?this.restartVideo():null}
您可以看到这个JSFIDLE,我尝试根据您的代码制作一些类似的东西:
类VideoContainer扩展React.Component{
建造师(道具){
超级(道具);
此.state={
重新启动:false
}
}
componentDidMount(){
setTimeout(function(){this.setState({restart:true});}.bind(this),3000);
}
restartVideo(){
console.log('视频重新启动…')
}
render(){
返回(
{this.state.restart?this.restartVideo():null}
/>
)
}
}
ReactDOM.render(
,
document.getElementById('容器')
);
如果你在这行加上括号,我希望这对你有所帮助:
onProgress={()=>{this.state.restart?this.restartVideo():null}
您可以看到这个JSFIDLE,我尝试根据您的代码制作一些类似的东西:
类VideoContainer扩展React.Component{
建造师(道具){
超级(道具);
此.state={
重新启动:false
}
}
componentDidMount(){
setTimeout(function(){this.setState({restart:true});}.bind(this),3000);
}
restartVideo(){
console.log('视频重新启动…')
}
render(){
返回(
{this.state.restart?this.restartVideo():null}
/>
)
}
}
ReactDOM.render(
,
document.getElementById('容器')
);
我希望你能帮到你谢谢你的努力!同样,通过执行this.restartVideo(this)
我能够在函数restartVideo(ref)
中调用ref.player.seek(0)
,如果this
和ref
实际上是一样的(听起来好像是一样的),那么就不用传递任何东西,只需使用this.player.seek(0)
在函数中。感谢您,此操作成功!同样,通过执行this.restartVideo(this)
我能够在函数restartVideo(ref)
中调用ref.player.seek(0)
,如果this
和ref
实际上是一样的(听起来好像是一样的),那么就不用传递任何东西,只需使用this.player.seek(0)
在函数中。感谢您提供详细答案。有理由相信你的详细答案。有道理
class VideoContainer extends React.Component {
constructor(props) {
super(props);
this.state = {
restart: false
}
}
componentDidMount(){
setTimeout(function() { this.setState({restart: true}); }.bind(this), 3000);
}
restartVideo() {
console.log('Video restarting...')
}
render() {
return (
<video
width="400" controls
src="http://www.quirksmode.org/html5/videos/big_buck_bunny.webm"
onProgress={() => {this.state.restart ? this.restartVideo() : null}}
/>
)
}
}
ReactDOM.render(
<VideoContainer />,
document.getElementById('container')
);