I'm building a function at the moment which receives data from an API call, maps through it (it's an array of objects), and pushes an item to a respective array depending on one of its values.
However, it only seems to be pushing one item into each array and no more.
There are no errors in my code, so not sure what I'm doing wrong - I assume I'm overwriting the array each time but not sure.
Here's the state arrays which the items need to be pushed into:
const [backlog, setBacklog] = useState([])
const [inProgress, setInProgress] = useState([])
const [paused, setPaused] = useState([])
const [completed, setCompleted] = useState([])
And here is the function itself:
const fetchData = async () => {
await axios.get("/api/fetch/fetchData")
.then(async (response) => {
const data = await response.data;
setAllTasks(data)
data.map(item => {
if (item.status === 'backlog') {
setBacklog([...backlog, item])
} else if (item.status === 'in-progress') {
console.log(item)
setInProgress([...inProgress, item])
} else if (item.status === 'paused') {
setPaused([...paused, item])
} else if (item.status === 'completed') {
setCompleted([...completed, item])
}
})
})
}