I am passing down a Redux prop to a component that is sourced from a fetch payload. The payload successfully changes state in my reducer and according to my Redux DevTools the component that I am trying to pass the state down to as a prop successfully receives the prop (an array of objects that is fetched in the action) yet when I console.log the prop or try to do anything with it it shows up as an empty array.
My index reducer:
export default combineReducers({
cart: cartitemReducer,
products: productReducer,
})
The fetch action:
export const fetchCart = () => dispatch => {
fetch('http://localhost:3000/api/v1/cartitems/')
.then(res => res.json())
.then(cartjson =>
dispatch({
type: 'FETCH_CART',
payload: cartjson
}))
}
The reducer:
const initialState = {
userCart: [],
cartItem: {}
}
export default function(state = initialState, action) {
switch(action.type) {
case FETCH_CART:
return {
...state,
userCart: action.payload
};
mapping state to props:
const mapStateToProps = state => ({
cart: state.cart.userCart
})
When I console.log(this.props.cart) it returns an empty array. Yet my DevTools correctly show the contents of the array under cart.userCart