1

I have below react-native code that I am using to render a text of array elements using map.

import React from 'react';
import { createStackNavigator } from '@react-navigation/stack';
import {Card} from 'react-native-elements';
import {View, Text} from 'react-native';


class Contact extends React.Component{
    constructor(props){
        super(props);
        this.state={
            arr:["Vipul","Satish","Manisha","Jonty"]
        }
    }
    render(){
        return(
            <Card>
                <Card.Title>Contact Information</Card.Title>
                <Card.Divider />
                <View>
                    {
                        this.state.arr.map(function(elem){
                            console.log(elem)
                            return(
                                <Text>Hello {elem}</Text>
                            );
                        })
                    }
                </View>
            </Card>
        );
    }
}

export default Contact;

I already have array setup with state in constructor of my class. First I was using arrow function to return <Text>. But it wasn't working. Now regular function isn't working too. I am getting below error:

Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.

However, If I replace my render function with this:

render(){
   return(<View><Text>Hello world</Text></View>);
}

then everything is working fine and I am getting Hello world on screen. Also, the console.log in the above code is correctly printing all the array elements but it is just that the elements are not being rendered.

I have checked other topics similar to this but couldn't get any help.

Thank You! The state is this:

constructor(props){
        super(props);
        this.state={
            arr:["Vipul","Satish","Manisha","Jonty"]
        }
    }

EDIT: This is what has been given on their official website:

<Card>
  <Card.Title>CARD WITH DIVIDER</Card.Title>
  <Card.Divider/>
  {
    users.map((u, i) => {
      return (
        <View key={i} style={styles.user}>
          <Image
            style={styles.image}
            resizeMode="cover"
            source={{ uri: u.avatar }}
          />
          <Text style={styles.name}>{u.name}</Text>
        </View>
      );
    })
  }
</Card>

You can see that my code is just similar to this one, so it should work that way.

4
  • I am 99% sure that the problem is in your state structure. Please, post it here. Commented Nov 18, 2020 at 12:52
  • Had it been a problem with state, the console.log wouldn't have printed elements. Commented Nov 18, 2020 at 12:53
  • The state does seem correct. Can you please post <Card />? I see that you render Card, and then Card.Divider, Card.Title etc. How could that be? Commented Nov 18, 2020 at 12:56
  • I saw that on their official website. Commented Nov 18, 2020 at 12:59

1 Answer 1

1

not sure what lib you are using for Card but it may take a title prop not a string as a child.

Sign up to request clarification or add additional context in comments.

8 Comments

I am using react-native-elements for Card
are you showing all the code? i suspect the issue is somewhere not being shown
I must say that the problem lies in the map itself because as I said, if I remove everything from render and just return hello world, it is working fine.
see the official docs of react-native card:reactnativeelements.com/docs/card
After scratching my head for an hour, I have found that the problem lies in <Card.Title>. I think it should take title as a prop.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.