【发布时间】:2018-06-15 11:24:08
【问题描述】:
当我尝试在 reactjs 中选择时发生了一些奇怪的事情。 这是我的代码:
onDropDownChange=(e)=>{
let val = this.props.contactUsReducer.contactUsList.filter(function(item) {
return item.topic == e.target.value
});
let key= e.target.value;
let name= e.target.name;
console.log("key "+key+" val: "+val[0].displayName+" name:"+name);
this.setState(prevState => {
return ( {...prevState, [name]:key, displayName: val[0].displayName})
}, function () {
console.log(this.state);
});
}
在我的渲染中我有
<select type="select" className="text-input flex-row-right-item" onChange={this.onDropDownChange} name="topic" value={this.state.displayName}>
{this.createSelectItems(this.props.contactUsReducer.contactUsList)}
</select>
这也是我的辅助函数:
createSelectItems=(itemsList)=> {
let items = [];
for (let i = 0; i < itemsList.length; i++) {
items.push(<option key={itemsList[i].topic} value={itemsList[i].topic}>{itemsList[i].displayName}</option>);
}
return items;
}
现在,当我尝试更改选择框时,UI 中不会发生任何变化,因此选择框不会显示更新后的选择,但我清楚地看到状态已更新。为什么更改没有反映在选择框中。
有什么想法吗?
【问题讨论】:
-
您正在渲染
this.props.contactUsReducer.contactUsList,这与组件的状态无关。
标签: javascript reactjs