I'm using Redux Toolkit but I can't seem to find a way to add a new value of an array of objects.
Here's my reducer:
import { createSlice, current } from "@reduxjs/toolkit"
const initialState = {
positions: []
}
export const userSlice = createSlice({
name: "user",
initialState,
reducers: {
setPositionsAction(state, { payload }) {
state.positions = payload
},
addPositionAction(state, { payload }) {
state.positions.push(payload)
}
}
})
export const {
setPositionsAction,
addPositionAction
} = userSlice.actions
export default userSlice.reducer
I'm dispatching the request after a successful axios request
dispatch(addPositionAction(response))
But the state gets deleted and only contains the new value I just added, removing completely all the data that was containing before.
What am I missing?