reactjs - 构建一个选择选项下拉列表:对象作为 React 子项无效
问题描述
我有以下 React JSX 代码片段,它从值列表中呈现选择选项下拉列表。
renderRoleDropdown(user_role) {
let values = ['a', 'b', 'c', 'd'];
return (
<select onChange={() => /* some logic here */ }>
{
values.map((value, _i) => {
let label = getDisplayLabelForValue(value);
return <option key={value} value={value}>{label}</option>;
})
}
</select>
);
}
据我所知,这符合其他示例的工作方式——只需循环一个列表并构建一个<option>
元素数组。但是,当我运行它时,我得到
对象作为 React 子对象无效(找到:带有键 {options} 的对象)。如果您打算渲染一组子项,请改用数组。
我没有使用对象,而且我显然返回了一个数组,所以想知道为什么它不喜欢这样?
谢谢!
解决方案
You had a lot syntax error (pascal names, no chars) - now it's works. You can also use value instead label.
I can't reflect the rest of the code - I don't know its purpose.
import React, { useState, useEffect, Component } from 'react'
let RenderRoleDropdown = () => {
let values = ['a', 'b', 'c', 'd'];
return (
<select>
{
values.map((value, _i) => {
let label = "random value"
return <option key={value} value={value}>{label}</option>;
})
}
</select>
);
}
export default RenderRoleDropdown;
推荐阅读
- error-handling - 有人可以用简单的 hello world 项目帮助我解决错误 winmain@16
- apache-spark - 当我在服务器上运行实例时,我可以在本地浏览器中获得用于 spark 的 Web UI 吗?
- c++ - 我需要平均 3 个数字
- excel - 如何防止在 Do While 循环中合并的单元格将我选定的单元格从 VBA 中的一列移动到另一列?
- html - 为什么我们使用“*”选择器而不是正文元素
- python - scrapy shell 无法在 Firefox 中打开响应
- c# - task.run 和 async 之间的区别
- angular - xplat 您在预期流的位置提供了无效对象。您可以提供 Observable、Promise、Array 或 Iterable
- docker - 如何在 Docker Compose 中连接不同网络中的容器?
- ios - 如何阻止 Swift 在我的 [String] 中注入转义的单引号?