你好,我正试图在导航器的右键中绑定一个函数,

但它给出了错误.

这是我的代码:

import React, { Component } from 'react';
import Icon from 'react-native-vector-icons/FontAwesome';
import Modal from 'react-native-modalbox';
import { StackNavigator } from 'react-navigation';
import {
   Text,
   View,
   Alert,
   StyleSheet,
   TextInput,
   Button,
   TouchableHighlight
} from 'react-native';

import NewsTab from './tabs/news-tab';
import CustomTabBar from './tabs/custom-tab-bar';

export default class MainPage extends Component {
    constructor(props) {
        super(props);  
    }

    alertMe(){
        Alert.alert("sss");
    }

    static navigationOptions = {
        title: 'Anasayfa',
        headerRight: 
            (<TouchableHighlight onPress={this.alertMe.bind(this)} >
                <Text>asd</Text>
             </TouchableHighlight>)        
    };

    render() {
        return(
            <View>

            </View>
        );
    }
}

然后得到如下错误:

undefined不是对象(计算'this.alertMe.bind')

在这个函数中,我无法处理渲染选项,但我无法使用它.我能为这个问题做些什么.

推荐答案

您应该在导航器功能中使用此选项

static navigationOptions = ({ navigation }) => {
    const { params = {} } = navigation.state;
    return {
        title: '[ Admin ]',
        headerTitleStyle :{color:'#fff'},
        headerStyle: {backgroundColor:'#3c3c3c'},
        headerRight: <Icon style={{ marginLeft:15,color:'#fff' }} name={'bars'} size={25} onPress={() => params.handleSave()} />
    };
};

使用componentwillmount,这样它就可以表示调用函数的位置.

componentDidMount() {
this.props.navigation.setParams({ handleSave: this._saveDetails });
}

然后你可以在函数中写下你的逻辑

_saveDetails() {
**write you logic here for **
}

**如果使用此函数,则无需绑定函数**

React-native相关问答推荐

无法执行 JS 调用:__fbBatchedBridge 未定义

使用 React Native 正确处理注销

在 create-react-native-app 元素中突然看到错误Plugin/Preset files are not allowed to export objects, only functions

React-Native:无法从我的 android 应用程序打开设备设置

如何在react-native 中从 FlatList 中删除元素/索引?

在选项卡更改时react-navigation

react-native redux 和 ListView

React Native WebView 中的 Javascript console.log()

React Native 入门元素Bundle 失败,出现 Unexpected Token 错误

带有 SafeAreaView-wrapper 的 react-native Modal 不起作用

如何使用 React Navigation 使 TabNavigator 按钮推送模态屏幕

React-Native iOS - 如何通过按下按钮从 React-Native 视图导航到非 React-Native 视图(本机 iOS 视图控制器)?

以编程方式在 React Native 中添加组件

React Navigation - 标题的渐变 colored颜色

React Native ScrollView 在snapping后重新回到顶部

Typescript:onPress 类型

你如何从 React Native 的 EventEmitter 实例中移除一个监听器?

react-native fetch 和基本身份验证

FlatList vs map react-native

在 webview 中检测touch 是 Apple Pencil 还是手指(react-native)