如何解决React Native中“文本字符串必须在<Text>组件内渲染”错误?

问题描述 投票:0回答:1

我正在使用 React Native 构建一个应用程序,但出现一个错误,提示“文本字符串必须在组件内呈现”错误,即使我没有在组件外部呈现任何文本。 这是发生错误的部分。


import React from "react";
import { SafeAreaView, StyleSheet, View } from "react-native";
import TopBar from "./Topbar";
import BottomNavBar from "./BottomNavbar";

const Layout = ({ children }) => {
  console.log("Children prop:", children);
  return (
    <SafeAreaView style={styles.container}>
      <TopBar />
      <View style={styles.contentContainer}>
        {children} {/* This is where the page-specific content will go */}
      </View>
      <BottomNavBar />
    </SafeAreaView>
  );
};
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#000000",
  },
  contentContainer: {
    flex: 1,
    marginBottom: 60, // Adjust to ensure content isn't hidden behind the navbar
  },
});

export default Layout;

import React from "react";
import {
  View,
  Text,
  TouchableOpacity,
  StyleSheet,
  ImageBackground,
  Image,
  Dimensions,
} from "react-native";
import Layout from "../layout/Layout"; // Import the Layout component

const { width, height } = Dimensions.get("window");

const Home = () => {
  return (
    <Layout>
      <ImageBackground
        source={require("../../../assets/home.jpg")}
        style={styles.backgroundImage}
      >
        <View style={styles.overlay} />

        <TouchableOpacity style={styles.button}>
          <Text style={styles.buttonText}>Enjoy The Game</Text>
        </TouchableOpacity>

        <TouchableOpacity style={styles.button}>
          <Text style={styles.buttonText}>Find Your Match</Text>
        </TouchableOpacity>

        <TouchableOpacity style={styles.button}>
          <Text style={styles.buttonText}>Plan Your Date</Text>
        </TouchableOpacity>
      </ImageBackground>
    </Layout>
  );
};

const styles = StyleSheet.create({
  backgroundImage: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
    width: width,
    height: height,
  },
  overlay: {
    position: "absolute",
    top: 0,
    left: 0,
    right: 0,
    bottom: 0,
    backgroundColor: "rgba(0, 0, 0, 0.4)",
  },
  button: {
    width: width * 0.8,
    padding: 15,
    borderRadius: 50,
    borderWidth: 2,
    borderColor: "#ffffff",
    justifyContent: "center",
    alignItems: "center",
    marginVertical: 10,
    backgroundColor: "rgba(255, 255, 255, 0.1)",
  },
  buttonText: {
    fontSize: 20,
    color: "#ffffff",
    fontFamily: "Playfair",
  },
  cursorIcon: {
    position: "absolute",
    width: 30,
    height: 30,
    right: -35,
    top: -10,
  },
});

export default Home;

我试图通过注释掉页面中的每个组件来找出错误的根源。 该错误似乎出现在导航栏组件中的 childs 属性中。 我这里没有在组件外部呈现任何文本。 有人可以帮忙吗?

javascript react-native components
1个回答
0
投票

您可以删除“{/* 这是页面特定内容的位置 */}”部分吗?那就试试吧。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.