【发布时间】:2018-05-06 23:20:39
【问题描述】:
我不确定为什么 e.target.value 的值是未定义的。
type State = {
filterCountry: string,
};
export default class Developers extends Component {
constructor(props) {
super(props);
this.state = {
developers: [],
};
}
componentDidMount() {
fetch('API').then(features => {
return features.json();
}).then(data => {
let developers = data.features.map((info) => {
const developer_info = {
id: info.id,
name: info.properties.name,
skills: info.properties.skills_full,
location: info.properties.location,
description: info.properties.description,
email: info.properties.email,
country: info.properties.continent
}
return developer_info;
});
this.setState({ developers: developers})
})
}
filter(e) {
this.setState({filter: e.target.value})
}
filterCountry(e){
this.setState({filterCountry: e.target.value})
}
render() {
if(this.state.filterCountry){
developers = developers.filter( developer =>
developer.country
.includes(this.state.filterCountry))
}
return (
<ControlSelect
id="country-select"
onChange={this.filterCountry.bind(this)} value={this.state.filterCountry}
options={options_dd2}
/>
</div>
试图遵循这个例子 create a dropdown in react to show/hide fetch data based on object class
【问题讨论】:
-
什么是
ControlSelect?它是否设置了e.target.value属性?显示完整的e内容。 PS:如果您发布正确缩进的代码也会有所帮助。 -
为什么值是
this.state.filterCountry并且不在状态构造函数中 -
我们可以忽略ControlSelect,它没有设置e.target.value属性
-
@liam 我不确定.. 我有点糊涂了,.. 不是 100% 确定一切
-
好吧,在你的构造器中创建一个像
filterCountry: 'value'这样的filterCountry状态。
标签: javascript reactjs filter dropdown