react-native - router-flux 导航栏按钮 this.function 不是一个函数

标签 react-native navbar react-native-router-flux

我在我的 router-flux 导航栏中实现了一个像这样的右键:

  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 不是函数

我在构造函数中声明了我的函数 this.getCurrentPosition = this.getCurrentPosition.bind(this)

最佳答案

您使用的是static方法,这意味着您无权访问this,它仅适用于实例。

所以你可以

1) 删除静态

renderRightButton = () => {
          return (
              <TouchableOpacity onPress={() => this.getCurrentPosition()} style={{bottom: 4, right: 8}}>
                  <Icon name='ios-locate-outline' style={{color: 'white'}} />
              </TouchableOpacity>
          );
    }

2) 或将 getCurrentPosition 作为参数传递给 render 方法。

static renderRightButton = (props, getCurrentPosition) => {
          return (
              <TouchableOpacity onPress={getCurrentPosition} style={{bottom: 4, right: 8}}>
                  <Icon name='ios-locate-outline' style={{color: 'white'}} />
              </TouchableOpacity>
          );
    }

关于react-native - router-flux 导航栏按钮 this.function 不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44408864/

相关文章:

react-native - React Native TextInput ref 始终未定义

android - 在本地主机上 react native android 连接后端服务

css - .net mvc 默认应用程序,导航菜单不是水平的

javascript - 使用 setTimeout 导航到另一个组件不会运行

javascript - react native 路由器通量 : How to style NavDrawer's button and navigation bar?

windows - react-native 和 Visual Studio Emulator for Android

javascript - React-Native:在输入时创建一个新的 TextInput 行

css - ie7 中的导航栏悬停问题

ios - NavBar 按钮不会拖到 UIViewController 的右上角

javascript - 如何在 React Native Router Flux 中嵌套场景并使用方向 ='vertical' 导航?