简体   繁体   English

我正在尝试实现产品的添加和删除,但出现此错误:Uncaught TypeError: props.handleFavouriteClick is not a function

[英]I am trying to implement the addition and removal of the product but I get this error:Uncaught TypeError: props.handleFavouriteClick is not a function

this my file ProductCard.jsx Here I added a handleFavouriteClick to be equivalent to the passed function and to use it in the add and remove files这是我的文件 ProductCard.jsx 在这里我添加了一个 handleFavouriteClick 以等效于传递的 function 并在添加和删除文件中使用它


export default function ProductCard(props) {
  const { id, title, pictures, sizes,  } = props.product
  const [favored, toggleFavored] = useFavorite(id)

 

  return (
   <div className="group">
       <Heart
          className={cn(
            'h-4 w-4 mt-1 cursor-pointer stroke-1',
            favored
              ? 'stroke-red-400 fill-red-400 animate-like'
              : 'stroke-gray-600 ',
          )}
          onClick={()=> props.handleFavouriteClick(props.product)}
        />
      </div>
      <Link to={`/products/${id}`}>
        <h3 className="mt-2 text-base font-light text-gray-600">{title}</h3>
      </Link>
    </div>
  )
}

and this is Home.jsx.here I passed to handleFavouriteClick the function adding a product imported from another file.when I click on the heart, the above error comes out这是 Home.jsx.here 我传递给 handleFavouriteClick function 添加从另一个文件导入的产品。当我点击心脏时,出现上述错误

  const theme = useTheme();
  const { data } = useSearchProductsQuery();
  const isTablet = useMediaQuery(theme.breakpoints.down("md"));
  const [addFavouriteProduct] = useFavorite(product)
 return (
    <Container className="pl-10 pr-10">
      <Banner />
      <InsChapter/>
       <Chapter/>
       <Chapter2/>
      {isTablet ? (
    <SearchFilter/>
    ): null}
      <div style={{display:'flex', justifyContent:'center'}}>
  {isTablet ? null : (
         <SearchFilterSideBar/>
       )}     
      <div className="grid grid-cols-2 mt-4 gap-y-5 gap-x-2 sm:gap-y-10 sm:gap-x-4 lg:grid-cols-3">
        {React.Children.toArray(
          data?.content.map(product => <ProductCard handleFavouriteClick={addFavouriteProduct} product={product}/>),        
        )}  
      </div>
      </div>
    </Container>
  ) 
}

Please help me.请帮我。

I think useFavorite() maybe returns many functions and variables.我认为 useFavorite() 可能会返回许多函数和变量。 So if you want to use addFavouriteProduct function from useFavorite(), you should write like this.所以如果你想使用 useFavorite() 中的 addFavouriteProduct function,你应该这样写。

const {addFavouriteProduct} = useFavorite(product)

Please try this.请试试这个。 Best最好的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试获取类型为checbox的输入的Attribute,但它不断出现此错误:Uncaught TypeError:a.getAttribute不是一个函数 - I am trying to get Attribute of input of type checbox but it keeps getting this error: Uncaught TypeError: a.getAttribute is not a function 我有这个错误-&gt;未捕获的类型错误:_this.props.login is not a function - I have this error -> Uncaught TypeError: _this.props.login is not a function 我正在尝试实施谷歌自动完成搜索,但面临问题 - 未捕获类型错误:无法设置未定义的属性“predictionsNew”? - I am trying to implement google autocomplete search, but facing issue - Uncaught TypeError: Cannot set property 'predictionsNew' of undefined? 为什么会出现“未捕获的TypeError”错误? - Why am I getting 'Uncaught TypeError' error? 使用车把我得到这个错误:Uncaught TypeError:$(…).geocomplete不是一个函数 - Using handlebar I get this error :Uncaught TypeError: $(…).geocomplete is not a function 我正在尝试检索表数据,但出现此错误: Uncaught TypeError: result.rows.product is not a function at products.html:134 - I'm trying to retrieve table data and i'm getting this error: Uncaught TypeError: result.rows.product is not a function at products.html:134 我试图获得使用return函数的附加值,但没有得到| 您能帮助我我到底在做什么错吗? - I am trying to get the addition value of using return function but not getting | Could you help to me what exactly I am doing wrong? 为什么我得到 Uncaught TypeError: $(...).DataTable is not a function - Why I get Uncaught TypeError: $(…).DataTable is not a function 为什么我得到 TypeError: _this.props.&quot;functionName&quot; is not a function ReactJS - Why am I getting TypeError: _this.props."functionName" is not a function ReactJS 为什么我会收到错误-indexOf未捕获的TypeError - Why i get a error - indexOf Uncaught TypeError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM