元素类型无效,需要字符串反应本机移动设备

发布于 2025-01-14 16:01:58 字数 6733 浏览 2 评论 0原文

你好,我的代码出现一些错误,我已经尝试了一些解决方案,但仍然不起作用,有人可以帮助我吗?谢谢

App.tsx

import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { SafeAreaView, StyleSheet, View } from 'react-native';
import { ThemeProvider, Div, Text } from 'react-native-magnus';
import DashboardBody from './src/Component/Dashboard/DashboardBody';
import { Route } from './src/Routes/Route';


export default function App() {
  return (
    <ThemeProvider>
      <Route />
    </ThemeProvider>
  );
}

Route.js

import React from "react";
import { NavigationContainers } from "@react-navigation/native";
import { createStackNavigator } from "@react-navigation/stack";
import { BodyBar } from "../Component/BottomNav/BodyBar";

const RouteStack = createStackNavigator();
export const Route = () => {
  return (
    <NavigationContainers>
      <RouteStack.Navigator>
        <RouteStack.Screen
          name="Lobby"
          component={BodyBar}
          options={{ headerShown: false }}
        />
      </RouteStack.Navigator>
    </NavigationContainers>
  );
};

BodyBar.js

import React from "react";
import {
  Alert,
  Animated,
  StyleSheet,
  TouchableOpacity,
  View,
} from "react-native";
import { CurvedBottomBar } from "react-native-curved-bottom-bar";
import Ionicons from "react-native-vector-icons/Ionicons";
import {
  heightPercentageToDP as hp,
  widthPercentageToDP as wp,
} from "react-native-responsive-screen";
import DashboardBody from "../Dashboard/DashboardBody";
import User from "../Profile/User";

export const BodyBar = () => {
  const _renderIcon = (routeName: string, selectedTab: string) => {
    let icon = "";

    switch (routeName) {
      case "title1":
        icon = "home";
        break;
      case "title2":
        icon = "person";
        break;
    }

    return (
      <Ionicons
        name={icon}
        size={25}
        color={routeName === selectedTab ? "#feae3b" : "gray"}
      />
    );
  };
  const renderTabBar = ({ routeName, selectedTab, navigate }: any) => {
    return (
      <TouchableOpacity
        onPress={() => navigate(routeName)}
        style={{
          flex: 1,
          alignItems: "center",
          justifyContent: "center",
        }}
      >
        {_renderIcon(routeName, selectedTab)}
      </TouchableOpacity>
    );
  };

  return (
    <View style={{ flex: 1 }}>
      <CurvedBottomBar.Navigator
        style={styles.bottomBar}
        strokeWidth={0.5}
        height={hp(8)}
        circleWidth={hp(8)}
        bgColor="#78c040"
        initialRouteName="title1"
        borderTopLeftRight
        swipeEnabled={false}
        renderCircle={({ selectedTab, navigate }) => (
          <Animated.View style={styles.btnCircle}>
            <TouchableOpacity
              style={{
                flex: 1,
                justifyContent: "center",
              }}
              onPress={() => Alert.alert("Click Action")}
            >
              <Ionicons name={"qr-code"} color="gray" size={hp(5)} />
            </TouchableOpacity>
          </Animated.View>
        )}
        tabBar={renderTabBar}
      >
        <CurvedBottomBar.Screen
          name="title1"
          position="left"
          component={DashboardBody}
        />
        <CurvedBottomBar.Screen
          name="title2"
          component={User}
          position="right"
        />
      </CurvedBottomBar.Navigator>
    </View>
  );
};

export const styles = StyleSheet.create({
  container: {
    flex: 1,
    padding: 20,
  },
  button: {
    marginVertical: 5,
  },
  bottomBar: {},
  btnCircle: {
    width: wp(15),
    height: hp(8),
    borderRadius: hp(4),
    alignItems: "center",
    justifyContent: "center",
    backgroundColor: "white",
    padding: hp(1),
    shadowColor: "#000",
    shadowOffset: {
      width: 0,
      height: 0.5,
    },
    shadowOpacity: 0.2,
    shadowRadius: 1.41,
    elevation: 1,
    bottom: 30,
  },
  imgCircle: {
    width: 30,
    height: 30,
    tintColor: "gray",
  },
  img: {
    width: 30,
    height: 30,
  },
});

DashboardBody.js

import React from "react";
import { Button, Div, Icon, Image, Text } from "react-native-magnus";
import HeaderLobby from "../Reuseable/HeaderLobby";
import {
  heightPercentageToDP as hp,
  widthPercentageToDP as wp,
} from "react-native-responsive-screen";

const SPACING = hp(1.5);

const DashboardBody = () => {
  return (
    <Div bg="gray400" flex={1}>
      <HeaderLobby headerText={"Dashboard"} />
      <Div
        bg="#78c040"
        h={hp(17)}
        style={{
          borderBottomLeftRadius: hp(15),
          borderBottomRightRadius: hp(15),
        }}
      >
        <Text textAlign="center" color="white" fontSize={hp(3)}>
          Hai, Mike
        </Text>

        <Text
          textAlign="center"
          mt={SPACING}
          fontSize={hp(2.5)}
          fontWeight="bold"
          color="white"
        >
          Produk Saya
        </Text>
        <Text
          textAlign="center"
          mt={SPACING}
          fontSize={hp(4)}
          fontWeight="bold"
          color="white"
        >
          0
        </Text>
      </Div>

      <Button
        w={wp(90)}
        h={hp(15)}
        rounded={hp(5)}
        ml={hp(2.5)}
        mt={SPACING}
        prefix={
          <Icon
            name="battery-charging"
            fontFamily="Ionicons"
            fontSize={hp(7)}
            color="#feae3b"
            // h={hp(10)}
            // w={wp(13)}
            mr={hp(2)}
          />
        }
        fontSize={hp(4)}
        fontWeight="bold"
        bg="white"
      >
        <Div>
          <Text fontSize={hp(3)} w={wp(50)} fontWeight="bold">
            Deskripsi Produk
          </Text>
          <Text w={wp(50)}>
            Lihat informasi 
          </Text>
        </Div>
      </Button>
    </Div>
  );
};

export default DashboardBody;

User.js

import { View } from "react-native";
import React from "react";
import { Div, Text } from "react-native-magnus";
import HeaderBack from "../Reuseable/HeaderBack";

const User = () => {
  return (
    <Div>
      <HeaderBack HeaderText={"Profile"} />
      <Text>Name</Text>
    </Div>
  );
};

export default User;

但是当我不使用 Route 部分时,代码仍然可以工作,就像您可以使用 DashboardBody 和 User 一样。

但是当使用Route idk时为什么代码错误

Hallo i got some Error with my code i already try some of the solution but still not work, can someone help me? Thank You

App.tsx

import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { SafeAreaView, StyleSheet, View } from 'react-native';
import { ThemeProvider, Div, Text } from 'react-native-magnus';
import DashboardBody from './src/Component/Dashboard/DashboardBody';
import { Route } from './src/Routes/Route';


export default function App() {
  return (
    <ThemeProvider>
      <Route />
    </ThemeProvider>
  );
}

Route.js

import React from "react";
import { NavigationContainers } from "@react-navigation/native";
import { createStackNavigator } from "@react-navigation/stack";
import { BodyBar } from "../Component/BottomNav/BodyBar";

const RouteStack = createStackNavigator();
export const Route = () => {
  return (
    <NavigationContainers>
      <RouteStack.Navigator>
        <RouteStack.Screen
          name="Lobby"
          component={BodyBar}
          options={{ headerShown: false }}
        />
      </RouteStack.Navigator>
    </NavigationContainers>
  );
};

BodyBar.js

import React from "react";
import {
  Alert,
  Animated,
  StyleSheet,
  TouchableOpacity,
  View,
} from "react-native";
import { CurvedBottomBar } from "react-native-curved-bottom-bar";
import Ionicons from "react-native-vector-icons/Ionicons";
import {
  heightPercentageToDP as hp,
  widthPercentageToDP as wp,
} from "react-native-responsive-screen";
import DashboardBody from "../Dashboard/DashboardBody";
import User from "../Profile/User";

export const BodyBar = () => {
  const _renderIcon = (routeName: string, selectedTab: string) => {
    let icon = "";

    switch (routeName) {
      case "title1":
        icon = "home";
        break;
      case "title2":
        icon = "person";
        break;
    }

    return (
      <Ionicons
        name={icon}
        size={25}
        color={routeName === selectedTab ? "#feae3b" : "gray"}
      />
    );
  };
  const renderTabBar = ({ routeName, selectedTab, navigate }: any) => {
    return (
      <TouchableOpacity
        onPress={() => navigate(routeName)}
        style={{
          flex: 1,
          alignItems: "center",
          justifyContent: "center",
        }}
      >
        {_renderIcon(routeName, selectedTab)}
      </TouchableOpacity>
    );
  };

  return (
    <View style={{ flex: 1 }}>
      <CurvedBottomBar.Navigator
        style={styles.bottomBar}
        strokeWidth={0.5}
        height={hp(8)}
        circleWidth={hp(8)}
        bgColor="#78c040"
        initialRouteName="title1"
        borderTopLeftRight
        swipeEnabled={false}
        renderCircle={({ selectedTab, navigate }) => (
          <Animated.View style={styles.btnCircle}>
            <TouchableOpacity
              style={{
                flex: 1,
                justifyContent: "center",
              }}
              onPress={() => Alert.alert("Click Action")}
            >
              <Ionicons name={"qr-code"} color="gray" size={hp(5)} />
            </TouchableOpacity>
          </Animated.View>
        )}
        tabBar={renderTabBar}
      >
        <CurvedBottomBar.Screen
          name="title1"
          position="left"
          component={DashboardBody}
        />
        <CurvedBottomBar.Screen
          name="title2"
          component={User}
          position="right"
        />
      </CurvedBottomBar.Navigator>
    </View>
  );
};

export const styles = StyleSheet.create({
  container: {
    flex: 1,
    padding: 20,
  },
  button: {
    marginVertical: 5,
  },
  bottomBar: {},
  btnCircle: {
    width: wp(15),
    height: hp(8),
    borderRadius: hp(4),
    alignItems: "center",
    justifyContent: "center",
    backgroundColor: "white",
    padding: hp(1),
    shadowColor: "#000",
    shadowOffset: {
      width: 0,
      height: 0.5,
    },
    shadowOpacity: 0.2,
    shadowRadius: 1.41,
    elevation: 1,
    bottom: 30,
  },
  imgCircle: {
    width: 30,
    height: 30,
    tintColor: "gray",
  },
  img: {
    width: 30,
    height: 30,
  },
});

DashboardBody.js

import React from "react";
import { Button, Div, Icon, Image, Text } from "react-native-magnus";
import HeaderLobby from "../Reuseable/HeaderLobby";
import {
  heightPercentageToDP as hp,
  widthPercentageToDP as wp,
} from "react-native-responsive-screen";

const SPACING = hp(1.5);

const DashboardBody = () => {
  return (
    <Div bg="gray400" flex={1}>
      <HeaderLobby headerText={"Dashboard"} />
      <Div
        bg="#78c040"
        h={hp(17)}
        style={{
          borderBottomLeftRadius: hp(15),
          borderBottomRightRadius: hp(15),
        }}
      >
        <Text textAlign="center" color="white" fontSize={hp(3)}>
          Hai, Mike
        </Text>

        <Text
          textAlign="center"
          mt={SPACING}
          fontSize={hp(2.5)}
          fontWeight="bold"
          color="white"
        >
          Produk Saya
        </Text>
        <Text
          textAlign="center"
          mt={SPACING}
          fontSize={hp(4)}
          fontWeight="bold"
          color="white"
        >
          0
        </Text>
      </Div>

      <Button
        w={wp(90)}
        h={hp(15)}
        rounded={hp(5)}
        ml={hp(2.5)}
        mt={SPACING}
        prefix={
          <Icon
            name="battery-charging"
            fontFamily="Ionicons"
            fontSize={hp(7)}
            color="#feae3b"
            // h={hp(10)}
            // w={wp(13)}
            mr={hp(2)}
          />
        }
        fontSize={hp(4)}
        fontWeight="bold"
        bg="white"
      >
        <Div>
          <Text fontSize={hp(3)} w={wp(50)} fontWeight="bold">
            Deskripsi Produk
          </Text>
          <Text w={wp(50)}>
            Lihat informasi 
          </Text>
        </Div>
      </Button>
    </Div>
  );
};

export default DashboardBody;

User.js

import { View } from "react-native";
import React from "react";
import { Div, Text } from "react-native-magnus";
import HeaderBack from "../Reuseable/HeaderBack";

const User = () => {
  return (
    <Div>
      <HeaderBack HeaderText={"Profile"} />
      <Text>Name</Text>
    </Div>
  );
};

export default User;

But when i don't use Route part the code can still work, like you can use DashboardBody and User.

But when use Route idk why the code error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呢古 2025-01-21 16:01:58

NavigationContainer 组件上存在拼写错误。重构代码如下:


import React from "react";
import { NavigationContainer } from "@react-navigation/native";
import { createStackNavigator } from "@react-navigation/stack";
import { BodyBar } from "../Component/BottomNav/BodyBar";


const RouteStack = createStackNavigator();
export const Route = () => {
  return (
    <NavigationContainer>
      <RouteStack.Navigator>
        <RouteStack.Screen
          name="Lobby"
          component={BodyBar}
          options={{ headerShown: false }}
        />
      </RouteStack.Navigator>
    </NavigationContainer>
  );
};

There's a typo mistake on the NavigationContainer component. Refactor code as below:


import React from "react";
import { NavigationContainer } from "@react-navigation/native";
import { createStackNavigator } from "@react-navigation/stack";
import { BodyBar } from "../Component/BottomNav/BodyBar";


const RouteStack = createStackNavigator();
export const Route = () => {
  return (
    <NavigationContainer>
      <RouteStack.Navigator>
        <RouteStack.Screen
          name="Lobby"
          component={BodyBar}
          options={{ headerShown: false }}
        />
      </RouteStack.Navigator>
    </NavigationContainer>
  );
};

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文