Reputation: 1
I am fairly new at using context API and hooks in react. am facing a problem that the initial state is not updating while trying to pass a dispatch of action using a reference of useReducer
, eventhought the objects are passed to it. The code are as follow:
reducer.js
export const initialState={
basket:[ ],
}
const reducer= (state= initialState, action) => {
console.log(action);
switch(action.type) {
case 'ADD_TO_BASKET':
return {
...state,
basket:[...state.basket,action.item]
};
case 'REMOVE_FROM_BASKET':
//we cloned the basket
let newBasket = [...state.basket];
const index= state.basket.findIndex((basketItem) => basketItem.id === action.id);
if (index >= 0) {
//item exist in basket , remove it
newBasket.splice(index, 1);
} else {
console.warn (
'cant remove product (id: ${action.id}) as its not in basket'
);
}
return {...state, basket: newBasket,};
break;
default:
return state;
}
}
export default reducer;
the console.log(action)
in above is giving item: {…} id: "101" image: "https://images.pexels.com/photos/534/animal-countryside-agriculture-farm.jpg" price: 11.96 rating: 5 title: "dress1" <prototype>: Object { … } type: "ADD_TO_BASKET" <prototype>: Object { … }
so the object is getting passed to the reducer but the initial state is not updating. only the case:ADD_TO_BASKET is not working the other case: REMOVE_FORM_BASKET is working fine .I would really appreciate if anyone can help me out.
here is other files for refrence . stateProvider.js
//we need this to track the basket
//setup data layer
import React, { createContext , useContext, useReducer } from "react";
import reducer, {initialState} from './reducer';
//this is the data Layer
export const StateContext = createContext();
//BUILD a provider
export const StateProvider = ({children}) =>{
const state = useReducer(reducer, initialState);
return (
<StateContext.Provider value={state}>
{children}
</StateContext.Provider>
);
}
//this is how we use it inside of a component
export const useStateValue = () =>useContext(StateContext);
product.js
import { useStateValue } from './StateProvider';
import "./product.css";
function Product ({id,title,image,price,rating}){
const [{}, dispatch] = useStateValue();
const addToBasket =() => {
console.log("am clicked");
dispatch({
type: 'ADD_TO_BASKET',
item: {
id: id,
title: title,
image: image,
price: price,
rating: rating
},
});
};
return (......
......
.....
header.js
import "./Header.css";
import {Link} from "react-router-dom"
import { useStateValue } from './StateProvider';
function Header() {
//const [state,dispach] = useStateValue();
const [{basket,user}] = useStateValue();
console.log(basket);
return(
<div>
.
.
.
<Link to="/checkout" className="header_link">
<div className="header_optionbasket">
<ShoppingBasketIcon/>
<span className="header_optiontwo basketcount">{basket.length}</span>
</div>
</Link>
.
.
.
</div>
)
after the intial state is updated the basket.length in header.js was soupose to update, but it remains 0 but if I hardcode a object in initialState>basket then the the basket.length is getting updated. but the objects are not getting added to the InitialState > basket
Upvotes: 0
Views: 1339
Reputation: 65
export const initialState={
basket:[ ],
}
const reducer= (state=initialState, action) => {
console.log(action);
switch(action.type) {
case 'ADD_TO_BASKET':
return {
...state,
basket:[...state.basket,action.item]
};
You have to initialize the state in reducer. Also you dont need to add Break statements in reducer.
Upvotes: 1