React-native Picker选择了文本问题

时间:2018-04-05 18:32:16

标签: react-native

当我在Picker组件中选择一个项目时,状态值会发生变化,但视觉选择项目不会更改。

enter image description here

enter image description here

enter image description here

constructor(props) {
    super(props);
    this.state = {
        localSelecionado: { value: null, index: 0 }
    };
}

<Picker
    style={{ alignItems: 'center' }}
    itemStyle={{ alignItems: 'center' }}
    selectedValue={this.state.localSelecionado}
    onValueChange={(itemValue, itemIndex) => this.setState({ localSelecionado: { value: itemValue, index: itemIndex } })}>

    <Picker.Item key={null} label="Selecione..." value={0} />
    {this.props.lista.map((local) => {
        return (
            <Picker.Item key={local._id} label={local.nome} value={local._id} />
        )
    })}

</Picker>
<Label style={{ alignSelf: 'center' }}>Index: {this.state.localSelecionado.index}</Label>

1 个答案:

答案 0 :(得分:0)

您正在selectedValue传递一个对象,您应该将selectedValue={this.state.localSelecionado}更改为selectedValue={this.state.localSelecionado.value}

希望这会有所帮助!