React Native React导航标题按钮事件

时间:2017-08-09 17:00:27

标签: react-native

您好我正在尝试在导航器右键中绑定一个函数,

但它给出了错误。

这是我的代码:

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')

当我在渲染函数中使用此方法时,它工作得很好但是在NavigatonOption中我无法处理它。我能为这个问题做些什么。

4 个答案:

答案 0 :(得分:30)

您应该在导航功能中使用它

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 **
}

**如果您使用**

,则无需绑定函数

答案 1 :(得分:2)

可能与上述相同...

   class LoginScreen extends React.Component {
      static navigationOptions = {
        header: ({ state }) => ({
          right: <Button title={"Save"} onPress={state.params.showAlert} />
        })
      };

      showAlert() {
        Alert.alert('No Internet',
          'Check internet connection',
          [
            { text: 'OK', onPress: () => console.log('OK Pressed') },
          ],
          { cancelable: false }
        )
      }

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

      render() {
        return (
          <View />
        );
      }
    }

答案 2 :(得分:2)

react-navigation v5 版本将是:

export const ClassDetail = ({ navigation }) => {
  const handleOnTouchMoreButton = () => {
    // ...
  };

  useLayoutEffect(() => {
    navigation.setOptions({
      headerRight: () => (
        <TouchableOpacity onPress={handleOnTouchMoreButton}>
          <Icon name="more" />
        </TouchableOpacity>
      ),
    });
  }, [navigation]);

  return (
    // ...
  )
}

答案 3 :(得分:0)

这是用于导航v4。您需要在功能组件之外修改导航选项。您的按钮事件需要通过导航传递参数。

[Category("Input")]
public InArgument<string> TemplatePath { get; set; }

[Category("Options")]
[DefaultValue(false)]
public bool Invocietodata { get; set; }

然后在功能组件中,可以使用该参数执行以下操作:

pageName['navigationOptions'] = props => ({
    headerRight:  ()=>
         <TouchableOpacity onPress={() => props.navigation.navigate("pageRoute", 
{"openAddPopover": true})  } ><Text>+</Text></TouchableOpacity>    })
相关问题