簡體   English   中英

如何使用 onClick 從 JSX 組件向反應函數組件傳遞參數

[英]How to pass arguments to a react function component from a JSX component using onClick

這是我的大代碼中的一個非常小的片段:

import React from "react";
const Home = () => {
    return (
      imgFilter.map((imgs) => {
        return ( <
          Col sm = "3"
          xs = "12"
          key = {
            imgs.id
          }
          className = "trend-image" >
          <
          img src = {
            imgs.path
          }
          alt = {
            imgs.id
          }
          className = "img-fluid"
          onClick = {
            Price
          }
          /> <
          /Col>
        )
      });
    }

在這段代碼中,您可以在一行中看到它寫成onClick={Price} 這里的 Price 是我正在導入的一個函數組件。 現在,Price 旨在接受爭論。 在這里,我想在單擊圖像時將imgs.id傳遞給價格。 我該怎么做

制作一個箭頭函數並使用imgs.id調用Price

import React from "react";
const Home = () => {
    return (
      imgFilter.map((imgs) => {
        return ( <
          Col sm = "3"
          xs = "12"
          key = {
            imgs.id
          }
          className = "trend-image" >
          <
          img src = {
            imgs.path
          }
          alt = {
            imgs.id
          }
          className = "img-fluid"
          onClick = {() =>  Price(imgs.id)}
          /> <
          /Col>
        )
      });
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM