找不到变量:createDrawerNavigator

Vic*_*ike 2 navigation-drawer react-native

我正在尝试在我的应用中实现抽屉导航。我发现这个网站(https://reactnavigation.org/docs/en/drawer-based-navigation.html)给出了一个实现的代码示例,但是当我运行它时它说

找不到变量:createDrawerNavigator。

所以这意味着缺少导入。但是,我似乎无法为createDrawerNavigator. 想知道是否有人可以帮我解决这个问题!React Native 的新手。

class MyHomeScreen extends React.Component {
    static navigationOptions = {
        drawerLabel: 'Home',
        drawerIcon: ({
            tintColor
        }) => ( <
            Image source = {
                require('./chats-icon.png')
            }
            style = {
                [styles.icon, {
                    tintColor: tintColor
                }]
            }
            />
        ),
    };

    render() {
        return ( <
            Button onPress = {
                () => this.props.navigation.navigate('Notifications')
            }
            title = "Go to notifications" /
            >
        );
    }
}

class MyNotificationsScreen extends React.Component {
    static navigationOptions = {
        drawerLabel: 'Notifications',
        drawerIcon: ({
            tintColor
        }) => ( <
            Image source = {
                require('./notif-icon.png')
            }
            style = {
                [styles.icon, {
                    tintColor: tintColor
                }]
            }
            />
        ),
    };

    render() {
        return ( <
            Button onPress = {
                () => this.props.navigation.goBack()
            }
            title = "Go back home" /
            >
        );
    }
}

const styles = StyleSheet.create({
    icon: {
        width: 24,
        height: 24,
    },
});

const MyApp = createDrawerNavigator({
    Home: {
        screen: MyHomeScreen,
    },
    Notifications: {
        screen: MyNotificationsScreen,
    },
});
Run Code Online (Sandbox Code Playgroud)

Ami*_*mir 5

您使用的每个子组件,首先必须导入它:正如Pritish Vaidva提到的,您必须添加

import { createStackNavigator, createDrawerNavigator } from "react-navigation";
Run Code Online (Sandbox Code Playgroud)

在你的文件的开头。