【发布时间】:2019-08-09 02:04:22
【问题描述】:
我目前正在尝试在 react 中呈现一个下拉列表,该列表将显示时间列表。用户应该能够选择一个特定的值,并且该值将通过 onChange() 在状态内更新。我的问题是我正在尝试使用选择,但是当我单击它时,我的下拉列表没有显示任何内容。任何帮助将不胜感激,谢谢。
这是我目前拥有的
我的文件.js
export default class MyClass extends Component {
constructor(props){
super(props);
this.state = {
times: ["1:00", "2:00", "3:00"]
}
}
RenderList(){
let tempArray = [];
let times = this.state.times;
for(var i = 0; i < times.length; i++){
tempArray.push(<option>{times[i]}</option>)
}
return tempArray
}
return (
<div
<select>{this.RenderList()}</select>
</div>
);
}
我有点希望在下拉列表中看到一个时间列表,但该列表为空。
【问题讨论】:
-
i < myArray,你将一个数字与一个数组进行比较,它应该是i < myArray.length -
@EmileBergeron 刚刚更新它并给我一个错误:无法读取未定义的属性“长度”
-
如果是这样,没有minimal reproducible example,我们无能为力。
-
@EmileBergeron 刚刚更新了您现在应该能够复制它的确切文件
-
您只是缺少
render函数才能正确呈现。但German's answer 是这里最好的方法,onChange处理程序也是如此。
标签: javascript html node.js reactjs