router-flux导航栏按钮this.function不是功能 [英] router-flux navbar button this.function is not a function
问题描述
我在router-flux导航栏中实现了一个像这样的右键:
I implemented in my router-flux nav bar a right button like this:
static renderRightButton = (props) => {
return (
<TouchableOpacity onPress={() => this.getCurrentPosition()} style={{bottom: 4, right: 8}}>
<Icon name='ios-locate-outline' style={{color: 'white'}} />
</TouchableOpacity>
);
}
但是,如果我单击我的按钮,则会出现此错误:this.5.getCurrentPosition is not a function
But If I click on my button I get this error :this.5.getCurrentPosition is not a function
我在构造函数中声明了我的功能,
this.getCurrentPosition = this.getCurrentPosition.bind(this)
I declare my function in the constructor with
this.getCurrentPosition = this.getCurrentPosition.bind(this)
推荐答案
您正在使用static
方法,这意味着您无权访问this
,该方法仅适用于实例.
You're using a static
method, which means you don't have access to this
, which only works for instances.
所以你可以
1)删除static
renderRightButton = () => {
return (
<TouchableOpacity onPress={() => this.getCurrentPosition()} style={{bottom: 4, right: 8}}>
<Icon name='ios-locate-outline' style={{color: 'white'}} />
</TouchableOpacity>
);
}
2)或将getCurrentPosition
作为参数传递给render方法.
2) or pass getCurrentPosition
to the render method as a parameter.
static renderRightButton = (props, getCurrentPosition) => {
return (
<TouchableOpacity onPress={getCurrentPosition} style={{bottom: 4, right: 8}}>
<Icon name='ios-locate-outline' style={{color: 'white'}} />
</TouchableOpacity>
);
}
这篇关于router-flux导航栏按钮this.function不是功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!