Reputation: 75
How can I add a userRating property of null to an array of objects using axios?
This is what I tried but it doesn't work
const movieRequest = () => {
axios
.get(key)
.then((response) => {
const updatedResponse = response.data
updatedResponse.map((movie) => ({
...movie,
userRating: null,
}))
setMovieList(updatedResponse)
})
.catch((error) => console.log(error))
}
Upvotes: 0
Views: 889
Reputation: 6280
map
returns a new array and does not mutate the array on which it is called. You just need to change your function to:
.then((response) => {
const updatedResponse = response.data.map((movie) => ({
...movie,
userRating: null,
}));
setMovieList(updatedResponse);
})
Upvotes: 3
Reputation: 63
.map returns a new array, you need to assign it to a variable.
const mapped = updatedRespone.map.... setMovieList(mapped)
Upvotes: 1