Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Type 'void' is not assignable to type 'ReactNode'

I cannot get the total because of the type of calculateTotal function

import CartItem from "./CartItem"
import {CartItemType} from '../App'
import {Typography,Stack} from '@mui/material';
type Props ={
    cartItem:CartItemType[];
    addToCart:(clickedItem: CartItemType)=>void;
    removeFromCart:(id:number) => void;
}
const Cart: React.FC<Props> = ({cartItem,addToCart,removeFromCart}) => {
  const calculateTotal=(items: CartItemType[])=>{
    items.reduce((ack:number,item)=>ack + item.amount* item.price, 0)
  }
  return (
    <Stack sx={{width:'25em'}}>
        <h4>Your Shopping Cart</h4>
        {cartItem.length ===0? <p>No items in cart.</p>: null}
        {cartItem.map(item=>(

        <CartItem key={item.id} item={item} addToCart={addToCart} removeFromCart={removeFromCart}/>
        ))}
        {/* Total */}
        <p>Total: ${calculateTotal(cartItem)}</p>
    </Stack>
  )
}

export default Cart

The calculateTotal function does not work couse it’s type

and that’s the error messege

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Type ‘void’ is not assignable to type ‘ReactNode’
This JSX tag’s ‘children’ prop expects a single child of type ‘ReactNode’, but multiple children were provided.

>Solution :

You need to return the value from calculateTotal function

const calculateTotal=(items: CartItemType[])=>{
  return items.reduce((ack:number,item)=>ack + item.amount* item.price, 0)
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading