【发布时间】:2018-08-29 15:19:11
【问题描述】:
我正在填充一个选择列表并从同一个 json 中绘制相应的信息组件。 Select 设置了 .map 和所有正确的值..(我认为)。 onChange 被分配给 countryChange ,它接受一个事件值和 setStates 来传递触发器。
我不明白的是, 1. e.target.value 是字符串而不是数字, 2.当一个state赋值为0时,e.target.value和state返回不同的值...
this.state={
selectedCountry:0
}
countryChange( e ) {
const list = e.target.value;
const countrylist = this.props.countries.find(c => c.id === list)
this.setState({
selectedCountry: countrylist
// selectedCountry: parseInt(e.target.value, 10)
});
}
render() {
return(
<select id="exampleSelect" onChange={this.countryChange.bind(this)}>
<option value="d">Select Country</option>
{this.props.countries.map((item) => ( <option key={item.id} value={item.id}> { item.name } </option> ))}</select>
之后,我将 selectedCountry 作为 props 发送给其他组件以呈现信息。我一直在寻找其他类似的问题和答案,并且走了这么远,但似乎无法按照我想要的方式完全执行它。
【问题讨论】: