Javascript 选择卡片元素时获取其ID

Javascript 选择卡片元素时获取其ID,javascript,arrays,reactjs,Javascript,Arrays,Reactjs,我有一个数组 plans = [ 0: {id: 2, identifier: "professional-2020", name: "professional", currency_code: "usd", price: "99.0"} 1: {id: 3, identifier: "free-2020", name: "free", currency_code: "

我有一个数组

plans = [
0: {id: 2, identifier: "professional-2020", name: "professional", currency_code: "usd", price: "99.0"}
1: {id: 3, identifier: "free-2020", name: "free", currency_code: "usd", price: "0.0"}
2: {id: 4, identifier: "basic-2020", name: "basic", currency_code: "usd", price: "29.0"}
3: {id: 5, identifier: "standard-2020", name: "standard", currency_code: "usd", price: "59.0"}
4: {id: 6, identifier: "custom-2020", name: "custom", currency_code: "usd", price: "999.0"}]
我已经从这个阵列中创建了一些卡

        <Card
          className="billing-plans__plans-card"
          size="normal"
          version="v2"
          title={plan.name}
          headerCenter={true}
          key={plan.id}
        >
          {matchPlan ? (
            <div className="billing-plans__current-plan-icon">
              <img src={greenCheck} alt="check" />
            </div>
          ) : null}
          <div className="billing-plans__plans-card-title">
            <sup>US$</sup>
            {plan.price}
            <sub>
              /<I18nText id="pricing.plans.misc.month" />
            </sub>
          </div>

          <div className="billing-plans__plans-card-actions">
            {!matchPlan ? (
              <button
                className="billing-plans__plans-card-actions-button"
                onClick={selectPlan}
              >
                <I18nText id="pricing.plans.actions.select" />
              </button>
            ) : null}
          </div>
        </Card>

{匹配计划(
):null}
美国$
{计划价格}
/
{!比赛计划(
):null}

现在我想在单击每张卡上的按钮时获取所选卡的ID。可能我需要编写一个函数并在onClick中调用它?

而没有真正的代码示例这就是我所能想到的:

constmycomponent=()=>{
const[selectedCard,setSelectedCard]=useState();
返回(
{/*非工作简化代码,请查看onClick fn*/}
{
设置所选卡(计划id)
}}
>
...
)
};

您应该创建一个最小的可复制示例(最好是在上)。这是否回答了您的问题?我应该如何使用
selectedCard
?您已经选择了状态为card,并且在您的内部有id。您可以随心所欲地使用它。您需要如何处理所选卡的ID?我将把所选卡的ID作为参数传递给put请求正文。这是我的put请求正文:
const selectPlan=async selectedCard=>{const{data}=wait httpClient.put(routes.billing.changePlan.update({id:selectedCardID,affiliate_id:affiliateId,}),如果(data.error==null){fetchplan();};
我将在id参数中传递所选的卡id