【问题标题】:Navigate in React-Native在 React-Native 中导航
【发布时间】:2017-08-14 23:44:32
【问题描述】:

我试图在 react-native 中导航,这是我的来源

import React from 'react';
import { StackNavigator } from 'react-navigation';
import { Text, TouchableOpacity } from 'react-native';

import LoginForm from './components/LoginForm';
import EmployeeList from './components/EmployeeList';
import EmployeeCreate from './components/EmployeeCreate';

const AddButton = (param) => (
  <TouchableOpacity onPress={() => console.log(param)}>
    <Text style={styles.addButtonStyle}>Add</Text>
  </TouchableOpacity>
);

const styles = {
  addButtonStyle: {
    fontSize: 18,
    marginRight: 25,
    color: '#007AFF'
  },
  headerTitleStyle: {
    fontWeight: 'normal',
    alignSelf: 'center',
    marginLeft: 50
  }
};

const RouterComponent = StackNavigator({
  EmployeeList: {
    screen: EmployeeList,
    navigationOptions: {
      title: 'Employee List',
      headerTitleStyle: styles.headerTitleStyle,
      headerLeft: null,
      headerRight: AddButton()
    }
  },
  EmployeeCreate: {
    screen: EmployeeCreate,
    navigationOptions: {
      title: 'Employee Create'
    }
  },
  Home: {
    screen: LoginForm,
    navigationOptions: {
      title: 'Please, Log In',
      headerTitleStyle: styles.headerTitleStyle
    }
  }
});

export default RouterComponent;

实际上我需要的是,在我的const AddButtonthis.props.navigation.navigate('EmployeeCreate'); 中,我可以从AddButton 导航到EmployeeCreate,但是AddButton 没有任何props。我试图将props 作为AddButton 中的参数,但没有发生任何好事。有谁知道,如何解决这个问题?

【问题讨论】:

    标签: javascript reactjs react-native react-navigation


    【解决方案1】:

    在 StackNavigator 的 EmployeeList 组件中,您可以这样定义 navigationOptions:

    navigationOptions: ({navigation}) => 
    return {
      title: 'Employee List',
      headerTitleStyle: styles.headerTitleStyle,
      headerLeft: null,
      headerRight: AddButton(navigation)
    }
    

    那么在AddButton组件中,已经可以使用navigation.navigate函数了。

    【讨论】: