I want to add items to the cart using redux, its not getting added neither its showing any error. please help me out. Below is my code
this is cartItem.js
export const ADD_TO_CART = 'ADD_TO_CART'
export const REMOVE_FROM_CART = 'REMOVE_FROM_CART'
const initialState = []
const cartItemsReducer = (state = initialState, action) => {
switch (action.type) {
case ADD_TO_CART:
return [...state, action.payload]
case REMOVE_FROM_CART:
return state.filter(cartItem => cartItem.id !== action.payload.id)
}
return state
}
export default cartItemsReducer
this is store.js
import { createStore } from 'redux'
import cartItemsReducer from './CartItem'
const store = createStore(cartItemsReducer)
export default store
this is App.js
import React from 'react'
import MainStackNavigator from './src/navigation/AppNavigator'
import { Provider as StoreProvider } from 'react-redux'
import store from './src/redux/Store'
export default function App() {
return (
<StoreProvider store={store}>
<MainStackNavigator />
</StoreProvider>
)
}
this is screen where I am dispatching
import ADD_TO_CART from '../redux/CartItem'
import { useDispatch } from 'react-redux'
const dispatch = useDispatch()
const addItemToCart = item => dispatch({ type: ADD_TO_CART, payload: item })
<TouchableOpacity onPress={() => addItemToCart(item)} style={styles.button}>
<Text style={styles.buttonText}>Add +</Text>
</TouchableOpacity>
You're invoking the function with an item parameter, but where is it coming from?
You must define the parameter and then pass it.
<TouchableOpacity onPress={(item) => addItemToCart(item)} style=
{styles.button}>