javascript - 文本节点不能是 <View> 的子节点

标签 javascript reactjs react-native

我想使用 ReactJS 创建一个表,所以这是我找到的代码:

import React, { Component } from 'react';
import {
  ScrollView,
  RefreshControl,
  StyleSheet,
  Text,
  SafeAreaView,
  View
} from 'react-native';

export default class Table extends Component {
    renderRow() {
        return (
            <View style={{ flex: 1, alignSelf: 'stretch', flexDirection: 'row' }}>
                <View style={{ flex: 1, alignSelf: 'stretch' }} /> { /* Edit these as they are your cells. You may even take parameters to display different data / react elements etc. */}
                <View style={{ flex: 1, alignSelf: 'stretch' }} />
                <View style={{ flex: 1, alignSelf: 'stretch' }} />
                <View style={{ flex: 1, alignSelf: 'stretch' }} />
                <View style={{ flex: 1, alignSelf: 'stretch' }} />
            </View>
        );
    }

    render() {
        const data = [1, 2, 3, 4, 5];
        return (
            <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
            {
                data.map((datum) => { // This will render a row for each data element.
                    return this.renderRow();
                })
            }
            </View>
        );
    }
}

但我收到一条错误消息 A text node cannot be a child of a <View> ,我不明白,因为没有<Text> <View> 中的元素并且循环看起来是有效的。

最佳答案

看来,如果您删除 renderRow() 中的注释,它就可以正常工作:

// Remove this comment
 { /* Edit these as they are your cells. You may even take parameters to display different data / react elements etc. */}

也许 RN 将其视为插入评论的纯文本。

关于javascript - 文本节点不能是 <View> 的子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62388817/

相关文章:

javascript - 从 ajax 调用返回一个值到父函数

javascript - 在 React.JS 中围绕组件共享对象

reactjs - react redux typescript : connect has missing type error

firebase - Expo, Firebase, 使用谷歌登录

android - 无效的资源目录名称,drawable-anydpi-v21

javascript - JQuery:在追加时得到一个 child

javascript - jQuery width() 未在具有生成内容的 div 上返回正确的值

javascript - 在 VueJS 2 中使用 v-for value + string 的动态 v-model

reactjs - 'children' Prop 的类型是什么?

reactjs - React useEffect刷新没有获取值