簡體   English   中英

如何在選項中傳遞額外的鍵來 react-dropdown 並從 onChange 道具中取回它們?

[英]How to pass extra keys in options to react-dropdown and get them back from the onChange prop?

我正在使用這個下拉庫react-dropdown

export const currencies = [
    { value: 'USD', label: '$ USD', currencySign: "$" },
    { value: 'INR', label: '₹ INR', currencySign: "₹" },
    { value: 'CAD', label: 'C$ CAD', currencySign: "C$"  }
]


<Dropdown 
    options={currencies}
    onChange={newSelectedOption => console.log(newSelectedOption)} // Getting an object with only 2 keys: "label" & "value". The third key "currencySign" is not there!
/>

有什么辦法可以在那個 onChange 對象中獲得“currencySign”?

謝謝!

2020 年 10 月 13 日更新:似乎 react-dropdown 庫不提供此功能。

react-dropdown庫似乎沒有帶來該功能,但您可以在onChange函數中執行以下操作:

const selectedCurrency = currencies.find({value, label} => value === newSelectedOption.value && label === newSelectedOption.label)

然后你可以檢索currencySign屬性

如果您可以訪問處理程序中的選項數組,則可以編寫一個輔助函數

Dropdown 
    options={currencies}
    onChange={onChange}
/>

onChange(selected){
  var itemWithCurrency = this.findSelected(selected.value,  this.currencies)
}

findSelected(value, items){
  return items.find(item => item.value === value)
}

暫無
暫無

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

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