skip to Main Content

How can I solve this error:

Error: Text strings must be rendered within a <Text> component

I just try to show activityindicator by using useState hook in react native but i get error

import React, { useEffect, useState } from 'react'
import { View, Text, StyleSheet } from 'react-native'
import { Card, Avatar, IconButton } from 'react-native-paper';
import Loading from '../Loader/Loading';

const About = () => {

  const [loading, setLoading] = useState(true);

  useEffect(() => {
    
    setTimeout(() => setLoading(false), 6000);

  }, []);

  return (
    <>
    {
      loading ? <Loading /> : 

        <View style={styles.container}>
          <Text>
          <Card style={{ padding: 100, backgroundColor: 'white' }}>
            <Card.Title title="About us" subtitle="Our profile"
            left={(props) => <Avatar.Icon {...props} icon="folder" />}
            right={(props) => <IconButton {...props} icon="dots-vertical" onPress={() => {}} />}
            />
            <Text>This is about us page</Text>
          </Card>
          </Text>
        </View>
    };

    </>
  )
}

export default About;

const styles = StyleSheet.create({
    container: {
        flex: 1,
        alignItems: 'center',
        justifyContent: 'center' 
    }
});

This is ActivityIndicator component which is imported on About component

import { ActivityIndicator , MD2Colors} from 'react-native-paper'

function Loading(){

    return <ActivityIndicator animating={true} color={MD2Colors.red800} />

}

export default Loading;

2

Answers


  1. The Text element can only contain text nodes or other Text elements.
    You cannot nest the Card element inside a Text element.

    Either remove the Text parent:

    <View style={styles.container}>
        <Card style={{ padding: 100, backgroundColor: 'white' }}>
            <Card.Title title="About us" subtitle="Our profile"
                left={(props) => <Avatar.Icon {...props} icon="folder" />}
                right={(props) => <IconButton {...props} icon="dots-vertical" onPress={() => {}} />}
            />
            <Text>This is about us page</Text>
        </Card>
    </View>
    

    Or remove the Card child

    Login or Signup to reply.
  2. In your example code, there is a ; after your JSX expression. Remove it and it’ll work.

    <>
    { // CODE };
    </>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search