I've been searching for a good answer for a while but couldn't find any. So I'm having this problem where I have a JSON file and I'd like to read the data from it in React.
The JSON looks somewhat like this:
{
"cats": [
{
"id": 1,
"name": "Cat1",
"age": 3
},
{
"id": 2,
"name": "Cat2",
"age": 2
}
],
"dogs": [
{
"id": 3,
"name": "Dog1",
"age": 5
},
{
"id": 4,
"name": "Dog2",
"age": 2
}
]
}
How can I read this JSON into a react state so that I can have all the cats and dogs and all their data but I can always tell if it was a cat or dog?
So right now I have 2 different states:
this.state = {
cats: data.cats,
dogs: data.dogs,
};
I can map these into a list but I don't have a way to tell what they are. Is there a way to keep what they are?
I hope it makes sense...
Thanks!