I’m a bit confused, is your array state variable an array? Because you’re trying to access array.list, which suggests that array is actually an object.
If you console.log(res) in your second .then, does it log an array or an object? What happens if you log res.list?
If you just want the array, another option would be to just save the array to state:
.then(res => setArray(res.list))
But I also think that “array” is a very poor variable name (even if it is an array). I would want something that makes it clear exactly what it is, like “events”, “recipes”, or “goodNamesForPoodles”. Avoid generic names. Good code tells a story, and good variable names make it easier to read that story.
Agree, it should be named after what it is and not what type of data it is.
Even having the data type in the name is often a bad idea in my opinion. So just name and not nameStr or users and not usersArr, or whatever else you can think of. If you do need something to denote the type use something that says less about the data type and more about what it is. So for example usersList or whatnot.
The reason being, just because you named it array and you expect it to contain an array doesn’t mean it’s actually an array. Adding the data type to the name just adds a false sense of security around the type.
Variable names are hard, don’t be afraid to refactor them as needed. It’s quick and easy using an editor and mostly safe as long as you pay attention.
Golden words there. It is one of the most important things you can do to make your code readable and can be very difficult to come up with the “perfect” name. But it’s worth it.
@veljkocukic Hopefully I’m not derailing the thread completely and you got some answers to your questions. If not, please post back with any questions you have.
The worst is when you know exactly what code you want to write and then you just get stuck at the variable name right at the beginning.
Sometimes I just say OK let’s not spend forever thinking about a good name right now, just call it something reasonable and come back to it. As you write more code the “true” name will often reveal itself as a reflection of the code you write.
And sometimes the name just has to change because it isn’t what it started out as any longer.