使用drawernavigator时,this.props.Navigation是不确定的



我将'react-navigation'的绘图纳维吉格分为本文档。但是,当我运行项目时,单击按钮时总是会出现此错误:

TypeError:未定义不是对象(评估 'this.props.navigation.navigate')

当我从左到右滑动时,什么也没发生,没有抽屉打开。

我已经检查了 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一起使用,还是我错过了config?

完整的源代码,这只是一个简单的示例项目,请查看:https://github.com/hcmus-iceteaviet-se/rn_tip_calculator

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

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

1)导入导航操作

import { NavigationActions } from 'react-navigation'

2)调度导航措施:

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

我对反应是新手,但有时这也发生在我身上。

我正在使用redux和stack导航器....但是这是我的工作示例...

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/master/app

发生这种情况,因为我不"连接"我的drawernavigator。

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

  • 第一种方法:将DrawernAvigator注册为App root组件。在index.js更改中:

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

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

    因此您可以删除App.js,因为它现在没有用。

  • 第二种方法:将寄存器App组件(在app.js中)作为应用程序root组件。然后将App组件放入"连接" DrawernAvigator到应用程序。

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

该文档对如何将导航器连接到应用程序,对寄存器组件或将导航器放入根组件中。这个驱动器新手像我疯狂!

相关内容

  • 没有找到相关文章

最新更新