Native-Base Picker无法正常运行Android - 不会触发onValueChange函数

时间:2017-12-16 20:50:01

标签: android react-native picker native-base

我正在使用Picker Native-Base组件作为我的react-native应用程序。在IOS上一切正常,而在Android方面,我无法触发我在onValueChange上添加的功能。

之前有没有人遇到过这个问题?

你是怎么解决的?我差不多花了一天时间。

这是我的代码。

<Picker style={{ width: 200, height: 40}}
                                    iosHeader="Branch"
                                    Header="Branch"
                                    mode="dropdown"
                                    textStyle={{color: 'white'}}
                                    placeholder="Branch"
                                    headerBackButtonText="Geri"
                                    selectedValue={this.state.selectedBranch}
                                    onValueChange={(value)=>this.onBranchSelected(value)}
                                >
                                    {this.state.branches.map((branches, i)=>{
                                            return(
                                                <Picker.Item label={branches.address_line} value={branches.id} key={i}/>
                                            );
                                        }
                                    )}
                                </Picker>

它不会在Android上调用函数onBranchSelected

2 个答案:

答案 0 :(得分:2)

I tried your code and was working fine for me.

Pasting my code

import React, { Component } from "react";
import { Platform } from "react-native";
import { Container, Header, Title, Content, Button, Icon, Text, Right, Body, Left, Picker, Form } from "native-base";

export default class PickerExample extends Component {
  constructor(props) {
    super(props);
    this.state = {
      branches: [
        { address_line: 'address 1', id: 1 },
        { address_line: 'address 2', id: 2 },
        { address_line: 'address 3', id: 3 },
        { address_line: 'address 4', id: 4 },
        { address_line: 'address 5', id: 5 }],
      selected1: 1
    };
  }

  onBranchSelected(value) {
    this.setState({
      selectedBranch: value
    });
  }

  render() {
    return (
      <Container>
        <Header>
          <Left>
            <Button transparent>
              <Icon name="arrow-back" />
            </Button>
          </Left>
          <Body>
            <Title>Picker</Title>
          </Body>
          <Right />
        </Header>
        <Content>
          <Form>
            <Picker
              style={{ width: 200, height: 40 }}
              iosHeader="Branch"
              Header="Branch"
              mode="dropdown"
              textStyle={{ color: 'grey' }}
              placeholder='Select branch'
              headerBackButtonText='Geri'
              selectedValue={this.state.selectedBranch}
              onValueChange={(value) => this.onBranchSelected(value)}
            >
              {this.state.branches.map((branches, i) => {
                return (
                  <Picker.Item label={branches.address_line} value={branches.id} key={i} />
                );
              }
              )}
            </Picker>
          </Form>
        </Content>
      </Container>
    );
  }
}

Gif

Dependencies

"native-base": "2.3.5",
"react": "16.0.0",
"react-native": "0.50.0",

答案 1 :(得分:1)

这是Picker的已知问题。问题是尝试使用.map。我自己无法使用地图与Picker合作。我唯一能找到的是名为npm的{​​{1}}包,我可以使用react-native-smart-picker。有局限性。

我还尝试了其他引导框架,这是vanilla .map的一个问题。

继承人的链接.. https://www.npmjs.com/package/react-native-smart-picker

继承人我的github回购...... https://github.com/GavinThomas1192/motoMechanicMeeKanic/blob/master/App/Components/vehicleMakePicker.js

这是我实现它的代码。

react-native

更新以显示我如何处理无可扩展按钮

<ScrollView>
  <View style={{ flex: 1, marginTop: 20 }}>
    {this.state.makes.length > 1 ?
      <ScrollView>
        <SmartPicker
          expanded={this.state.expanded}
          selectedValue={this.state.selectedMake}
          label='Select Make'
          onValueChange={this.handleChange.bind(this)}>
          {
            this.state.makes.map((ele) => {
              return (<Picker.Item label={ele} value={ele}/>);
            })
          }
          <Picker.Item label='Select Make' value='Toyota'/>
        </SmartPicker>
        <Button block onPress={() => this.props.vehicleMake(this.state.selectedMake)}>
          <Text>Done</Text>
        </Button>
      </ScrollView> : <Spinner/>
    }
  </View>
</ScrollView>

enter image description here

enter image description here