检查`MaterialTopTabNavigator`的渲染方法

时间:2021-08-01 15:44:53

标签: react-native react-navigation react-navigation-v5 react-navigation-top-tabs

我想创建一个 Top Tab 导航器,并在我的设备上运行它时将两个屏幕 Badges.js 和 Studying.js 放入 Tab 中,我收到一个错误消息 “错误:元素类型无效:预期为string ...""检查`MaterialTopTabNavigator的渲染方法

TabScreen.js

import React from 'react';
import {View, Image, Text} from 'react-native';
import {createMaterialTopTabNavigator} from '@react-navigation/material-top-tabs';
import Badges from './Badge';
import Studying from './Studying';

const Tab = createMaterialTopTabNavigator();

const TabsScreen = () => {
  return (
    <Tab.Navigator>
      <Tab.Screen name="Badges" component={Badges} />
      <Tab.Screen name="Studying" component={Studying} />
    </Tab.Navigator>
  );
};
export default TabsScreen;

Badges.js

import React from 'react';
import {View, Image, Text} from 'react-native';

const Badges = () => {
  return (
    <View>
      <Text>Badges</Text>
    </View>
  );
};

export default Badges;

Studying.js

import React from 'react';
import {View, Image, Text} from 'react-native';

const Studying = () => {
  return (
    <View>
      <Text>Studying</Text>
    </View>
  );
};

export default Studying;

enter image description here

2 个答案:

答案 0 :(得分:2)

我遇到了类似的问题。这是 mi 的解决方案(npm 或 yarn 没有问题)

a) 卸载更多更新版本 6.0.0

yarn remove react-navigation/material-top-tabs

b) 在我的情况下安装这个版本 5.3.15

yarn add @react-navigation/material-top-tabs@5.3.15

enter image description here

c) 停止并开始您的项目和工作 enter image description here enter image description here

d) 继续官方文档 https://reactnavigation.org/docs/material-top-tab-navigator/#example

答案 1 :(得分:1)

确保以下所有软件包都具有相同的版本

"@react-navigation/material-top-tabs": "^6.0.1",
"@react-navigation/native": "^6.0.1",
"@react-navigation/stack": "^6.0.1",