【问题标题】:How to make a bottomtabnavigator in react-native?如何在 react-native 中制作底部导航器?
【发布时间】:2021-03-04 22:21:38
【问题描述】:

我一直在尝试为我的应用程序制作一个 BottomTabNavigator,但一直无法做到。请帮助我识别代码中的错误。

这是用 App.js 编写的代码

import * as React from 'react';

import  { createBottomTabNavigator }  from '@react-navigation/bottom-tabs';

import mapscreen from './screens/MapScreen';
import groupscreen from './screens/GroupScreen';


import { NavigationContainer } from '@react-navigation/native';

const Tab = createBottomTabNavigator();

export default () => (
         <NavigationContainer>
         
    <Tab.Navigator>

      <Tab.Screen name="map" component={mapscreen}/>

      <Tab.Screen name="group" component={groupscreen} />

      
    </Tab.Navigator>
         </NavigationContainer>
       );

这是用 MapScreen.js 编写的代码

import * as React from 'react';
import { View, Text,StyleSheet} from 'react-native';

export const mapscreen = () => {

    <View style = {styles.container}>
    <Text>
        Map will exist here!!
    </Text>
    </View>
};



const styles = StyleSheet.create({
    container: {
      flex: 1, 
      alignItems: 'center', 
      justifyContent: 'center',
      

    },
  });

这是在 GroupScreen.js 中编写的代码

import * as React from 'react';
import { View, Text,StyleSheet} from 'react-native';

export const groupscreen = () => {

    <View style = {styles.container}>
    <Text>
        group exist here!!
    </Text>
    </View>
};


const styles = StyleSheet.create({
    container: {
      flex: 1, 
      alignItems: 'center', 
      justifyContent: 'center',

    },
  });

【问题讨论】:

    标签: react-native react-navigation react-native-navigation bottomnavigationview react-navigation-bottom-tab


    【解决方案1】:

    工作应用:Expo Snack

    几乎没有语法错误,除了一切都很好。

    //App.js
    import * as React from 'react';
    
    import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
    
    import MapsScreen from './screens/MapsScreen';
    import GroupScreen from './screens/GroupScreen';
    
    import { NavigationContainer } from '@react-navigation/native';
    
    const Tab = createBottomTabNavigator();
    
    export default App=() => (
      <NavigationContainer>
        <Tab.Navigator>
          <Tab.Screen name="Map" component={MapsScreen} />
          <Tab.Screen name="Group" component={GroupScreen} />
        </Tab.Navigator>
      </NavigationContainer>
    );
    
    //MapsScreen.js
    import * as React from 'react';
    import { View, Text, StyleSheet } from 'react-native';
    
    export default  MapsScreen = ({navigation}) => {
      return <View style={styles.container}>
        <Text>Map will exist here!!</Text>
      </View>;
    };
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        alignItems: 'center',
        justifyContent: 'center',
      },
    });
    
    //GroupScreen.js
    import * as React from 'react';
    import { View, Text, StyleSheet } from 'react-native';
    
    export default GroupScreen = ({navigation}) => {
      return <View style={styles.container}>
        <Text>group exist here!!</Text>
      </View>;
    };
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        alignItems: 'center',
        justifyContent: 'center',
      },
    });
    

    【讨论】:

    • 我在我的编辑器上运行了您建议的代码。它没有成功并显示错误。我在下一条评论中提到了错误。
    • ERROR 20:19 Building JavaScript bundle: error ERROR 20:19 Unable to resolve module ./screens/MapsScreen from C:\Users\Ind\Desktop\webdev\Onkar\App.js:跨度>
    • 这些文件都不存在:*screens\MapsScreen(.native|.ios.expo.ts|.native.expo.ts|.expo.ts|.ios.expo.tsx|.native .expo.tsx|.expo.tsx|.ios.expo.js|.native.expo.js|.expo.js|.ios.expo.jsx|.native.expo.jsx|.expo.jsx|.ios .ts|.native.ts|.ts|.ios.tsx|.native.tsx|.tsx|.ios.js|.native.js|.js|.ios.jsx|.native.jsx|.jsx| .ios.json|.native.json|.json) *
    • screens\MapsScreen\index(.native|.ios.expo.ts|.native.expo.ts|.expo.ts|.ios.expo.tsx|.native.expo.tsx| .expo.tsx|.ios.expo.js|.native.expo.js|.expo.js|.ios.expo.jsx|.native.expo.jsx|.expo.jsx|.ios.ts|.native .ts|.ts|.ios.tsx|.native.tsx|.tsx|.ios.js|.native.js|.js|.ios.jsx|.native.jsx|.jsx|.ios.json| .native.json|.json)
    • 3 |从“@react-navigation/bottom-tabs”导入 { createBottomTabNavigator }; 4 | > 5 |从 './screens/MapsScreen' 导入 MapsScreen; | ^ 6 |从'./screens/GroupScreen'导入GroupScreen; 7 | 8 |从 '@react-navigation/native' 导入 { NavigationContainer };
    猜你喜欢
    • 2021-07-04
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    相关资源
    最近更新 更多