代码之家  ›  专栏  ›  技术社区  ›  Bomber

React Navigationv2,导航功能不在标题道具中

  •  0
  • Bomber  · 技术社区  · 6 年前

    我已经准备好了 StackNavigator 就像这样:

    const AppNavigator = StackNavigator(
        {
            Home: {
                screen: HomeScreen
            },
            Month: {
                screen: Month
            },
            Day: {
                screen: Day
            }
        },
        {
            headerMode: "screen",
            navigationOptions: {
                header: props => <CustomHeader {...props} />
            }
        }
    );
    

    我可以使用 this.props.navigation.navigate("Month");

    但是,在我的 CustomHeader ,我看不到这个道具 navigate 调用。我需要 导航 使用标题中的按钮返回主屏幕。

      resetForm() {
        const {
          resetForm,
          clearCredentials,
          navigation
        } = this.props;
    
      this.props.navigation.navigate("Home");
    
    
      }
    

    如何访问 导航 移动到另一个屏幕的道具?

    客户标题 全部:

    import React, { Component } from "react";
    
    import { connect } from "react-redux";
    import {
      Image,
      View,
      Text,
      Modal,
      Button,
      TouchableOpacity,
      AsyncStorage,
      StyleSheet,
      Platform,
      Alert,
      TouchableHighlight
    } from "react-native";
    import Icon from "react-native-vector-icons/MaterialCommunityIcons";
    import { NavigationActions } from "react-navigation";
    import {
      setYear,
      setStudent,
      setGroup,
      fetchCategories,
      resetForm,
      resetData,
      fetchEvents
    } from "../actions/events";
    
    class CustomHeader extends Component {
      constructor() {
        super();
    
        this.resetForm = this.resetForm.bind(this);
        this.fetchEvents = this.fetchEvents.bind(this);
        this.showAlert = this.showAlert.bind(this);
      }
    
      resetForm() {
        const navigateAction = NavigationActions.navigate({
          routeName: "Home",
    
          params: {},
    
          action: NavigationActions.navigate({ routeName: "Home" })
        });
    
        this.props.dispatch(navigateAction);
      }
    
      showAlert() {
        Alert.alert("Events refreshed");
      }
    
      fetchEvents() {
        const {
          fetchEvents,
          navigate,
          credentials: { group }
        } = this.props;
        resetData();
        fetchEvents(group);
        navigate("Month");
        this.showAlert();
      }
    
      render() {
        return (
          <View style={styles.container}>
            <TouchableOpacity onPress={this.resetForm}>
              <Image
                source={require("../img/logout.png")}
                style={{ width: 25, height: 30 }}
              />
            </TouchableOpacity>
    
            <TouchableOpacity onPress={this.fetchEvents}>
              <Image
                source={require("../img/refresh.png")}
                style={{ width: 20, height: 30 }}
              />
            </TouchableOpacity>
          </View>
        );
      }
    }
    
    const mapDispatchToProps = dispatch => {
      return {
        resetForm: () => dispatch(resetForm()),
        fetchEvents: id => dispatch(fetchEvents(id)),
        resetData: () => dispatch(resetData())
      };
    };
    
    const mapStateToProps = state => {
      return {
        categories: state.fetchCategories,
        isLoading: state.isLoading,
        credentials: state.setCredentials
      };
    };
    
    export default connect()(CustomHeader);
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   tuan.tran    6 年前

    你必须通过 navigation navigationOptions 在标题组件中使用。你的 AppNavigator 应该是这样的

    const AppNavigator = StackNavigator(
    {
        Home: {
            screen: HomeScreen
        },
        Month: {
            screen: Month
        },
        Day: {
            screen: Day
        }
    },
    {
        headerMode: "screen",
        navigationOptions: ({ navigation }) => ({
            header: props => <CustomHeader {...props} navigation={navigation}/>
        })
    }
    );
    

    客户标题

    ...
    resetForm() {
      const {navigation} = this.props;
      navigation.navigate("Home");
    }
    ...