当我从下拉列表中选择第一个值时,第一次 setState 设置为所选值(在这种情况下,第一个值是我从“请选择事件”以外的数据呈现的动态值)但它再次设置为 null,这是仅在下拉列表的第一个值时发生,对于其余值,它运行良好。
state = {
data:[],
isLoading: true,
eventtitle:'',
}
componentDidMount = () => {
fetch(`${Vars.host}Multifilter/events?event_title=${this.state.eventtitle}`, {
method: 'GET'
})
.then((response) => response.json())
.then((responseJson) => {
this.setState({
data: responseJson,
isLoading: false
})
})
.catch((error) => {
console.error(error);
});
}
multifilter = () => {
console.log("this.state.eventtitle",this.state.eventtitle);
fetch(`${Vars.host}Multifilter/events?event_title=${this.state.eventtitle}`, {
method: 'GET'
})
.then((response) => response.json())
.then((responseJson) => {
this.setState({
data: responseJson
})
})
.catch((error) => {
console.error(error);
});
}
render(){
return (
<View>
<Picker
selectedValue={this.state.mainevent}
onValueChange={(itemValue, itemIndex) =>
this.setState({ mainevent: itemValue },() => {
this.multifilter();
})}>
<Picker.Item label='Please Select Events' value='' />
{
this.uniqueOptions(this.state.data, "main_event").map((name) => {
**following line returning event names but when I'm selecting first value then first time setState set the selected value but again it set to null, This is happening only with first value of dropdown**
return <Picker.Item label={name} value={name} key={name} />
})
}
</Picker>
<View>
)
}