Firebase data object storing - React native

Viewed 16

I am getting data like this from my firebase collection:

Object {
  "data": Object {
    "valuesArray": Array [
      "MCU",
      "Anime",
      "Shopping",
      "Travelling",
      "Cooking",
    ],
  },
}

I want to store valuesArrayin a bigger state object in react such as this:

const [data, setData] = useState<any>({});

The idea is that I store each data collection in this big state data object. I am finding it hard to reach the valuesArray since the data received has Object, then data, then Object again.

How can I accomplish storing only the valuesArray in the big state object?

1 Answers

I got there in the end by extracting the valuesArray right on the async function:

const createCategoriesArray: any = async () => {
  await db
    .collection("categories")
    .get()
    .then(querySnapshot => {
      return querySnapshot.docs.map(doc => {
        return {
          data: doc.data().valuesArray,
        };
      });
    })
    .then(data => {
      setCategories(data);
    })
    .catch(error => {
      console.log("Error getting documents: ", error);
    });
};
Related