gpt4 book ai didi

javascript - 如何从 React 元素(更改时)获取 "key"属性?

转载 作者:行者123 更新时间:2023-12-03 13:01:21 25 4
gpt4 key购买 nike

我想在选择onChange时获取选项的值和键。

我知道我可以在 onChangeOption 函数中使用 event.target.value 简单地获取选项的,但是我如何获取 key

<select onChange={this.onChangeOption} value={country}>
{countryOptions.map((item, key) =>
<option key={key} value={item.value}>
{item.name}
</option>
)}
</select>

最佳答案

您需要将 key 中的值作为不同的 prop 传递。

来自文档:

Keys serve as a hint to React but they don’t get passed to your components. If you need the same value in your component, pass it explicitly as a prop with a different name:

阅读:https://reactjs.org/docs/lists-and-keys.html

关于javascript - 如何从 React 元素(更改时)获取 "key"属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47070997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com