简体   繁体   中英

Type 'IProduct' is missing the following properties from type 'IProduct[]': length, pop, push, concat, and 29 more

I have two components parent and child, I want to pass data using props from props by using react and typescript.

Here are my interfaces

export interface IProduct {
        id: string;
        name: string;
        price: string;
        image: string;
        inStock: number;
        fastDelivery: boolean;
        ratings: number;
    }

    export interface IState {
        products: IProduct[];
        cart: {payload: number, qty: number}[];
    }

Here is the parent component contents

import Products from './Products';
import { CartContext, IProduct } from "../context/Context";

const Contents: React.FC =()=> {
  const { state:{products}, dispatch } = useContext(CartContext);
  return (
          <div className="home-content">
                {products.map((prod) =>{
                  console.log('prod', prod)
                  return (
                    <>
                      <Products products={prod} />
                    </>
                  )
                })}
       </div>
  )
}

export default Contents

Here is the child component

import { CartContext, IProduct } from "../context/Context";
import { IState } from "../reducers/CartReducer";

const Products: React.FC <IState>= ({products}): JSX.Element => {

  return (
    <div className="products_container">
      {products.map((item) => {
        console.log('item', item)        
      })}
    </div>
  )
}

I am getting the following error在此处输入图像描述

products.map((prod) => {
  return (
    <>
      <Products products={prod} />
    </>
  )
}
export interface IState {
  products: IProduct[];
}

You provide a single product prod , but the interface expects an array. Please read the error message carefully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM