如何通过我的反应导航

时间:2020-02-28 02:12:30

标签: javascript reactjs react-native navigation react-navigation

因此,我是React的新手,试图学习开发应用程序。我试图学习如何使用React Navigation,所以我安装了它和依赖关系,但是当我尝试使用它时,出现了一些错误。在运行npm start之后,第一个出现在cmd中:

Some of your project's dependencies are not compatible with currently installed expo package version:
 - react-native-screens - expected version range: 2.0.0-alpha.12 - actual version installed: ^2.0.0-alpha.12
Your project may not work correctly until you install the correct versions of the packages.
To install the correct versions of these packages, please run: expo install [package-name ...]

然后当我在Android sim卡中启动该应用程序时,我得到了:

Unable to resolve "@react-navigation/drawer" from "App.js"
Failed building JavaScript bundle.

这是我的代码:

import React from 'react';
import { StyleSheet, Text, View, Platform, Image, ImageBackground } from 'react-native';
import {Button} from 'native-base';
import { render } from 'react-dom';
import Search from './src/search';
import Landing from './src/landing';
import {NavigationContainer} from '@react-navigation/native'
import {createDrawerNavigator} from '@react-navigation/drawer'
import {createStackNavigator} from '@react-navigation/stack'
import {createMaterialBottomTabNavigator} from '@react-navigation/material-bottom-tabs'
import {createMaterialTopTabNavigator} from '@react-navigation/material-top-tabs'


var myBackground = require('./assets/icons/landing.jpg');
const Drawer = createDrawerNavigator();
const Stack = createStackNavigator();
const MaterialBottomTabs = createMaterialBottomTabNavigator();
const MaterialTopTabs = createMaterialTopTabNavigator();


export default class App extends React.Component {
  state = {

  }

  createHomeStack = () =>
  <Stack.Navigator>
    <Stack.Screen  name="Feed"/>
  </Stack.Navigator>

  createTopTabs = () =>
  {
    return <MaterialTopTabs.Navigator>
      <MaterialTopTabs.Screen name="Tab1" Component={Landing}/>
    </MaterialTopTabs.Navigator>
  }

  render() {
  return (
    <View style={styles.container}>
      <NavigationContainer>
        <Drawer.Navigator>
          <Drawer.Screen name="Home" Component="Landing"/>
          <Drawer.Screen name="Search" Component="Search"/>
          <Drawer.Screen name="Home"/>
        </Drawer.Navigator>
      </NavigationContainer>
    </View>
  );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    marginTop: Platform.OS === "android" ? 50 : 0
  },
});

有人知道我为什么会出现这些错误吗?在开始之前,我重新安装了React Nav。 提前致谢!

Package.json:

{
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject"
  },
  "dependencies": {
    "@react-native-community/masked-view": "^0.1.5",
    "@react-navigation/native": "^5.0.8",
    "expo": "~36.0.0",
    "native-base": "^2.13.8",
    "react": "~16.9.0",
    "react-dom": "~16.9.0",
    "react-native": "https://github.com/expo/react-native/archive/sdk-36.0.0.tar.gz",
    "react-native-gesture-handler": "^1.5.6",
    "react-native-reanimated": "^1.4.0",
    "react-native-safe-area-context": "^0.6.0",
    "react-native-screens": "^2.0.0-alpha.12",
    "react-native-web": "~0.11.7"
  },
  "devDependencies": {
    "@babel/core": "^7.0.0",
    "babel-preset-expo": "~8.0.0"
  },
  "private": true
}

1 个答案:

答案 0 :(得分:1)

因此,在您的情况下(如pacakge.json所示),您尚未安装用于抽屉式和堆叠式的软件包。解决抽屉问题后,您将获得堆栈问题。请按照以下步骤操作,重现您的问题

有关React Navigation Version 5.x的更多信息

使用此安装抽屉包:

npm i -s @react-navigation/drawer

使用此安装导航堆栈:

npm i -s @react-navigation/stack

使用以下方法安装底部的材料标签:

npm i -s @react-navigation/material-bottom-tabs

使用以下方法安装“材料”顶部标签:

npm i -s @react-navigation/material-top-tabs
  • 您应按照上述所有步骤解决项目中的问题

安装上述所有依赖项后,您需要清理并重建项目,并从仿真器中卸载以前的版本,然后再次运行

About Tab Navigation

About Drawer Navigation