reactjs - reactjs, putting n options together in a select
问题描述
I have the following piece of jsx in my reactjs program,
<select onChange = {this.selectChange}>
<option>{this.state.array[0]}</option>
<option>{this.state.array[1]}</option>
<option>{this.state.array[2]}</option>
<option>{this.state.array[3]}</option>
</select>
Its actually much longer than this, I have 32 elements in the array so I'd like to find a more compact way of doing this. For example, I was thinking something like this:
<select onChange = {this.selectChange}>
{for (var n=0; n<32; n++) <option>{this.state.array[n]}</option>}
</select>
However that doesn't work. How might I do this? Thanks
解决方案
const options = this.state.array.map(op => {
return <option>{op}</option>
});
return (
<select onChange = {this.selectChange}>
{options}
</select>
);
为了提高可读性,您可以将它们分成另一个变量options
,然后在该变量中使用映射函数生成单个项目。
要回答您的问题,您的方法行不通是因为缺少return
密钥
推荐阅读
- javascript - Paho Javascript Mqtt 客户端(通过 Websockets)未连接到 Mosquitto Broker(托管在 Google Cloud VM 上)
- tensorflow - AttributeError:模块“tensorflow.python.pywrap_tensorflow”没有属性“TFE_MonitoringNewCounter0”
- java - “ [main] INFO expect4j.Expect4j - 处理 EOF null” 这个问题到底是什么?
- android - 在 Android 供应商软件中使用 AHardwareBuffer_fromHardwareBuffer()?
- php - 我想在 PHP 中使用 htaccess 从 url 中隐藏 id
- c++ - 为什么我的梯度下降算法不收敛?(对于逻辑回归)
- flutter - 如何使用 darkTheme 在颤动中更改主题:
- r - 如何使用 ggplot2 从条形图中的多列中绘制 3 个单个数字?
- java - 如何在maven中做多个生成源代码的主类
- python - 在不更改python中的数字的情况下将变量的类型从十六进制字符串转换为uint_32