A text node cannot be a child of a <View>

Viewed 4512

I want to create a table using ReactJS, so this is the code I found :

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>
        );
    }
}

but I got an error saying A text node cannot be a child of a <View>, I don't understand because there is no <Text> element in <View> and loop looks like valid.

2 Answers

It seems that if you remove the comment inside the renderRow() it works just fine:

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

Maybe RN sees that as a plain text insted of a comment.

Correct way of commenting in react is like this:

{/* your comment goes here*/}

when you are commenting with //,react will treat this as a text node. As well as react native docs mentions ,we cannot wrap text with <View> .

the correct way is :

<View>
   <Text>
     dummy text
   </Text>
</View>

wrong way :

<View>
     dummy text
</View>

Check react native docs

Related