如何基于react-native中的状态初始化选择器值

时间:2019-03-25 13:25:25

标签: javascript react-native state picker

我开发了一种用于设置白天和黑夜的选择器。我从API获取选择器值的当前时间,并尝试使用setState进行设置,但是每次它将显示“ day”作为值。我希望它应该根据传递的值显示。

    this.state= {
        time : null
    }


   timeMode = (Currenttime) => { //here if the passed value is 'night' then it should set the value to night. 
                                //Then when clicking on picker it should be both values in dropdown
   this.setState({ time: Currenttime }); // this causes infinite loop...
    return (
        <View style={Styles.paddingStyle}>
            <Picker
                selectedValue={this.state.time}
                mode="dropdown"
                onValueChange={(value) => this.setState({time: value})}
    >
                {this.DropdownValues()}
            </Picker>
        </View>
    )
}

DropdownValues = () => {
    const time = [
      'day',
      'night'
    ];
    return time.map(data => {
      return <Picker.Item label={data} value={data} />;
    });
};

0 个答案:

没有答案