使用DrawerNavigator时,this.props.navigation未定义

时间:2017-12-16 14:20:06

标签: android react-native react-native-android react-navigation

我将'react-navigation'的DrawerNavigator整合到我的项目this文档中。但是当我运行项目时,单击Button时总会出现此错误:

  

TypeError:undefined不是对象(评估   ' this.props.navigation.navigate&#39)

当我从左向右滑动时,没有任何事情发生,没有抽屉打开。

我已检查this.props,并且它始终在控制台中记录为空{}。 我尝试了很多解决方案,但它仍然没有用。

calculator.js

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

    static navigationOptions = {
        drawerLabel: 'Calculator',
        drawerIcon: ({ tintColor }) => (
            <Image
                source={require('./../../res/images/icon_calculator.png')}
                style={[styles.icon, {tintColor: tintColor}]}
            />
        ),
    };

    render() {
        return (
            <View style={styles.container}>
                <View>
                    <Text style={styles.title}>Tip Calculator</Text>
                </View>
                <Button
                    onPress={() => this.props.navigation.navigate("SettingsScreen")}
                    title="Go to settings"
                />
            </View>
        );
    }
}

module.exports = Calculator;

settings.js

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

    static navigationOptions = {
        drawerLabel: 'Settings',
        drawerIcon: ({ tintColor }) => (
            <Image
                source={require('./../../res/images/icon_settings.png')}
                style={[styles.icon, {tintColor: tintColor}]}
            />
        ),
    };

    render() {
        return (
            <View style={styles.container}>
                <Text>Settings</Text>
                <Button
                    onPress={() => this.props.navigation.goBack()}
                    title="Go back home"
                />
            </View>
        );
    }
}

module.exports = Settings;

navigation.js

import {
    DrawerNavigator
} from 'react-navigation';
import Calculator from './../components/calculator/calculator.js';
import Settings from './../components/settings/settings.js';


const RootDrawer = DrawerNavigator({
    CalculatorScreen: {
        path: '/',
        screen: Calculator
    },
    SettingsScreen: {
        path: '/sent',
        screen: Settings
    }
}, {
    initialRouteName: 'CalculatorScreen',
    drawerPosition: 'left'
});

export default RootDrawer;

App.js

export default class App extends Component<{}> {
  render() {
    return (
      <Calculator/>
    );
  }
}

index.js

import { AppRegistry } from 'react-native';
import App from './App';

AppRegistry.registerComponent('rn_tip_calculator', () => App);

我是否必须将StackNavigatorDrawerNavigator一起使用,或者我在配置中遗漏了哪些内容?

完整的源代码,它只是一个简单的示例项目,请看一下:https://github.com/HCMUS-IceTeaViet-SE/rn_tip_calculator

任何帮助将不胜感激。谢谢!

3 个答案:

答案 0 :(得分:1)

您可以使用dispatch api https://reactnavigation.org/docs/navigators/navigation-actions

1)导入导航动作

import { NavigationActions } from 'react-navigation'

2)调度导航动作:

const navigateAction = NavigationActions.navigate({
    routeName: 'SettingsScreen',
    params: {},
})

this.props.navigation.dispatch(navigateAction)

答案 1 :(得分:1)

我对React-Native很新,但有时这也发生在我身上。

我正在使用Redux和堆栈导航器....但这是我的工作示例......

import { StackNavigator } from 'react-navigation'
import { Animated, Easing } from 'react-native'
import LoginScreen from '../Containers/LoginScreen'
import LaunchScreen from '../Containers/LaunchScreen'
import HomeScreen from '../Containers/HomeScreen'
import SignUpScreen from '../Containers/SignUpScreen'
import SettingsScreen from '../Containers/SettingsScreen'
import VehicleCreateScreen from '../Containers/VehicleCreateScreen'
import styles from './Styles/NavigationStyles'

// Manifest of possible screens
const PrimaryNav = StackNavigator({
  LoginScreen: { screen: LoginScreen },
  LaunchScreen: { screen: LaunchScreen },
  HomeScreen: { screen: HomeScreen },
  SignUpScreen: { screen: SignUpScreen },
  SettingsScreen: { screen: SettingsScreen },
  VehicleCreateScreen: { screen: VehicleCreateScreen }
}, {
    // Default config for all screens
    headerMode: 'none',
    initialRouteName: 'LaunchScreen',
    navigationOptions: {
      headerStyle: styles.header
    },
    transitionSpec: {
      duration: 0,
      timing: Animated.timing,
      easing: Easing.step0,
    },
  },


)

export default PrimaryNav

然后从未连接到REDUX的组件

import React, { Component } from 'react';
import { Container, Content, List, ListItem, Icon, Text, Button, Left, Right, Badge } from 'native-base';
import { Image } from 'react-native'
import styles from './Styles/SideBarStyle';
// import backgroundImage from '../Images/vw.jpg'

const backgroundImage = require("../Images/vw.jpg");
const drawerImage = require("../Images/dirtyHandsDark.jpg");


export default class SideBar extends Component {
    constructor(props) {
        super(props);
    }
    render() {
        // *********** HERE WE DECLARE AN ARRAY TO RENDER LISTS FROM. THIS COULD ALSO BE LIST OF BIKES FROM STORE.. ***********
        const datas = [
            {
                name: "Home",
                route: "HomeScreen",
                icon: "settings",
                bg: "#C5F442",
            },
            {
                name: "Repair",
                route: "HomeScreen",
                icon: "settings",
                bg: "#C5F442",
            },
            {
                name: "My Profile",
                route: "SettingsScreen",
                icon: "settings",
                bg: "#C5F442",
            },

        ];


        return (
            <Container>
                <Content bounces={false} style={{ flex: 1, backgroundColor: "#fff", top: -1 }}>
                    <Image source={backgroundImage} style={styles.drawerCover}>
                        <Image square style={styles.drawerImage} source={drawerImage} />
                    </Image>
                    <List
                        dataArray={datas}
                        renderRow={data =>
                            // *********** CREATE NEW LIST ITEM ON CLICK NAVIGATE TO APPROPRIATE LISTITEM.SCREEN ***********
                            <ListItem button noBorder onPress={() => this.props.navigation.navigate(data.route)}>
                                <Left>
                                    <Icon active name={data.icon} style={{ color: "#777", fontSize: 26, width: 30 }} />
                                    <Text style={styles.text}>
                                        {data.name}
                                    </Text>
                                </Left>
                                {data.types &&
                                    <Right style={{ flex: 1 }}>
                                        <Badge
                                            style={{
                                                borderRadius: 3,
                                                height: 25,
                                                width: 72,
                                                backgroundColor: data.bg,
                                            }}
                                        >
                                            <Text style={styles.badgeText}>{`${data.types} Types`}</Text>
                                        </Badge>
                                    </Right>}
                            </ListItem>}
                    />
                </Content>
            </Container>
        );
    }
}

你可以看到我引用this.props.navigation.navigate没问题。 下面是我的回购参考。 https://github.com/GavinThomas1192/motoMechanicMeeKanic/tree/master/App

答案 2 :(得分:0)

这是因为我没有将我的DrawerNavigator“连接”到我的应用程序。

有两种方法可以实现这一目标:

  • 第一种方式:将DrawerNavigator注册为app root组件。在index.js更改:

    来自AppRegistry.registerComponent('rn_tip_calculator', () => App);

    AppRegistry.registerComponent('rn_tip_calculator', () => RootDrawer);

    所以你可以删除App.js,因为它现在没用了。

  • 第二种方式:将注册App组件(在App.js中)保存为app root组件。然后将App组件放入“连接”DrawerNavigator到应用程序。

    export default class App extends Component<{}> {
      render() {
        return (
          <RootDrawer/>
        );
      }
    }
    

document没有说明如何将导航器连接到应用程序,没有关于注册组件或将导航器放在根组件中。这个让我疯狂的新手!