Skip to content

reducer.js:23 Uncaught TypeError: Cannot read properties of undefined (reading 'type') i'm calulating total price of basket but it shows this error in inspect>> console .shows error in reducer,js >>>> " switch (action.type)" #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
minal322 opened this issue Aug 5, 2022 · 4 comments

Comments

@minal322
Copy link

minal322 commented Aug 5, 2022

No description provided.

@minal322 minal322 changed the title reducer.js:23 Uncaught TypeError: Cannot read properties of undefined (reading 'type') reducer.js:23 Uncaught TypeError: Cannot read properties of undefined (reading 'type') i'm calulating total price of basket but it shows this error in inspect>> console .shows error in reducer,js >>>> " switch (action.type)" Aug 5, 2022
@ubongn
Copy link

ubongn commented Aug 5, 2022

can I see the code?

@minal322
Copy link
Author

minal322 commented Aug 5, 2022

this one is reducer.js file

export const initialState = {
basket: [],
user : null

};

export const getBasketTotal = (basket) =>
basket?.reduce((amount,item) => item.price+amount,0);

/export const getBasketTotal = (basket) => {
const total = basket
.map((item) => item.price)
.reduce((acc, value) => acc - value, 0);
return total;
};
/

const reducer = (state, action) => {

switch (action.type) {
case "ADD_TO_BASKET":
return {
...state,
basket : [...state.basket, action.item],
};

  default:
    return state;
}

};

export default reducer;

@minal322
Copy link
Author

minal322 commented Aug 5, 2022

checkout.js >>>> here suybtotall.js file is called.

function Checkout() {
const [{ basket, user }, dispatch] = useStateValue();

return (



    <div>
      <h3>Hello, { user?.email}</h3>
      <h2 className="checkout_title">Your shopping Basket</h2>

      {basket.map(item => (
        <CheckoutProduct
          id={item.id}
          title={item.title}
          image={item.image}
          price={item.price}
          rating={item.rating}
        />
      ))}

    </div>
  </div>

  <div className="checkout_right">
       <Subtotall />
  </div>
</div>

);
}

export default Checkout;

@minal322
Copy link
Author

minal322 commented Aug 5, 2022

subtotall.js file >>>>
unction Subtotall() {
const history = useNavigate();
const [{ basket}, dispatch] = useStateValue();

return (


<CurrencyFormat
renderText={(value) => (
<>


Subtotal ({basket.length} items) : value



This order contains a gift

</>

  )}
  decimalScale={2}
  value = {getBasketTotal(basket)}    //error exist here
  displayType={"text"}
  thousandSeparator={true}
  prefix={"₹"}
/>

<button  onClick={e => history('/payment')}>Proceed to Checkout and Check Total Price </button>
); }

export default Subtotall;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants