Currently delving into Redux Toolkit using TypeScript and facing a roadblock that seems deceptively simple. Unfortunately, my current knowledge isn't enough to unravel this puzzle and I'm in need of some guidance.
The issue arises with an error message stating 'Property 'push' does not exist on type 'WritableDraft'.' when executing the code snippet below:
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { Product } from '../pages/Product/Types'
export interface CartState {
products: Product[]
}
const initialState: CartState = {
products: []
}
export const cartSlice = createSlice({
name: 'cart',
initialState,
reducers: {
addToCart: (state, action: PayloadAction<Product>) => {
state.push(action.payload) <=== ERROR HERE, WITH 'push'
}
}
})
// Commented out since it wasn't used
// export const { addToCart, decrement, incrementByAmount } = cartSlice.actions
export const { addToCart } = cartSlice.actions
export default cartSlice.reducer
I recognize that the push() method fails because 'state' is not recognized as an array, yet a solution remains elusive considering the existing code structure. Any insights would be highly valued; thank you!