javascript - 在 React-Native 中导航

标签 javascript reactjs react-native react-navigation

我尝试在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 AddButton 中到达 this.props.navigation.navigate('EmployeeCreate'); 让我能够导航到 EmployeeCreate 来自 AddButton,但 AddButton 没有任何 props。我尝试将 props 作为 AddButton 中的参数抛出,但没有发生任何好事。有人知道如何解决这个问题吗?

最佳答案

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

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

然后在AddButton组件中,你已经可以使用navigation.navigate函数了。

关于javascript - 在 React-Native 中导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45684606/

相关文章:

javascript - JS promise 未按预期解决

reactjs - React Native iOS 中的响应式布局

javascript - jQuery - 在 mousedown 上创建一个新的可拖动 div,然后可以在同一操作中拖动它

javascript - style.height 不工作 javascript

javascript - Android CSS spritesheet webkit 转换和动画的问题

javascript - 如何通过我的 passport.js 链传递参数?

javascript React Form onsubmit 保存提交的用户名

reactjs - React Material ui 表无法从行获取元素

javascript - Redux - 组件不使用 connect() 重新渲染

reactjs - 为什么在 React Native 中我的页面转换之前有一个白色的 "loading screen"?