Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting "Objects are not valid as a React child" , im using an array to render a collection of children, but its still not working

As the title says, i am getting this issue when calling the below function

const CustomAgenda = () => {
  return <ScrollView>{returnArray()}</ScrollView>;
};

It is calling the returnArray function

const returnArray = async () => {
  let set = [];
  console.log(6);
  await getJobs().then(() => {
    console.log(7);
    set = j.map(x => {
      return (
        <View>
          <Text> {x.itemType} </Text>
        </View>
      );
    });
  });
  await console.log(set);
};

At the end this console.log’s the following – Which is what it should be, but im still getting that same error

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[<View><Text> Carerre </Text></View>, <View><Text> Car </Text></View>, <View><Text> Car </Text></View>, <View><Text> Car </Text></View>, <View><Text> Car </Text></View>]

I believe the error could be located in these next two functions

The below function gets job UID from a database, then calls the getJobData function to return the information from each job

async function getJobs() {
  console.log(8);
  await firestore()
    .collection('Users')
    .doc(global.uid)
    .get()
    .then(async documentSnapshot => {
      console.log(9);
      console.log(10);
      await getJobData(documentSnapshot.get('jobs'));
    });
}

The getJobData function pushes the returned data to a global array named ‘j’

const getJobData = async jobList => {
  for (const i of jobList) {
    let item = {};

    await firestore()
      .collection('Jobs')
      .doc(i)
      .get()
      .then(documentSnapshot => {
        item.id = i;
        item.itemType = documentSnapshot.get('itemType');
        item.size = documentSnapshot.get('size');
        item.type = 'Pickup';
      })
      .then(() => {
        j.push(item);
      });
  }
};

The ‘j’ array returns the following – which is also correct.

[{"id": "fST5A2WzgUR66xxNdHAU", "itemType": "Carerre", "size": "Large", "type": "Pickup"}, {"id": "cKQLGqqtyyuxhO7ZuFLR", "itemType": "Car", "size": "Large", "type": "Pickup"}, {"id": "l3KCfGSKh3MCySje832f", "itemType": "Car", "size": "Large", "type": "Pickup"}, {"id": "B8byNmHdc6g5YGqTPovX", "itemType": "Car", "size": "Large", "type": "Pickup"}, {"id": "FQraUtV7uTDyvkux1Vkf", "itemType": "Car", "size": "Large", "type": "Pickup"}]

After logging numbers, it seems to be the calling of getJobs, as the logs happen in this order – 6 – error – 8 9 10 7

Thankyou in advanced for any help

>Solution :

You need to return set in returnArray. But that wont matter anyway. You also need to store the result in state anyway (of the data, not the components). You are trying to render a promise, which wont work — and is the ultimate cause of the error. You need to wait for the data, then store that in state, which will trigger a render.

The returnArray becomes unnecessary with this.

Try this

const CustomAgenda = () => {
  const [data, setData] = useState([])

  useEffect(() => {
      getJobs().then(data => setData(data))
  }, [])

  return <ScrollView>{
            data.map(x=> (
              <View>
                <Text> {x.itemType} </Text>
              </View>
            )}
         </ScrollView>;
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading