reactjs - 如何将数组传递给 ReactSelect
问题描述
我将它作为道具传递给了一个组件,但我很难将它传递给 ReactSelect 组件。
const TimeInput=(
<div>
<ReactSelect
className='react-select react-select-top'
classNamePrefix='react-select'
id='displayLanguage'
menuIsOpen={this.state.openMenu}
menuPortalTarget={document.body}
styles={reactStyles}
**options={this.props.times}**
clearable={false}
onKeyDown={this.handleKeyDown}
onMenuClose={this.handleMenuClose}
onMenuOpen={this.handleMenuOpen}
aria-labelledby='changeInterfaceLanguageLabel'
/>
</div>
)
当我尝试将时间数组传递给 ReactSelect 时出现此错误(请忽略之后的错误,因为这些是我尚未处理的函数,即。handleMenuClose:
解决方案
options
prop 接收带有键的 JSON 对象数组和value
,label
因此您需要将时间数组转换为该格式。
一种方法是映射
// Before your render function
const timeOptions = this.props.times.map((timeString) => {
// Ideally you can change the value to something different that is easier to keep track of like the UTC offset
return {
value: timeString,
label: timeString
}
}
);
// In render function
// Now pass in timeOptions as the prop for your ReactSelect
...
<ReactSelect
...
options={timeOptions}
...>
推荐阅读
- c++ - 警告隐式转换丢失整数精度
- react-native - 如何在 React-Native 的折线图中绘制 2 个数据
- python - 从数组中提取非“-1”索引的最快方法是什么?
- python - 如何从许多项目的缓冲区中产生单个项目并定期重新填充缓冲区?
- javascript - ES6 类 - 链选择
- python - 上下文管理器处理路径上的类型提示
- laravel - 试图获得非对象一对多关系的属性“名称”
- php - 使用 php 从 sql 数据库创建额外的组合 html 表
- javascript - 为什么要 {} 和 {};在 JS 中进行不同的评估?
- django - 在 amazon S3.in django app 上添加静态文件后,主引导滑块中加载缓慢的视频