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

Native-Base Picker does not work properly Android - Does not trigger function onValueChange

我正在为我的 react-native 应用程序使用 Native-BasePicker 组件。在 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

这是 Picker 的已知问题。问题是尝试使用 .map。我自己永远无法使用 Picker 进行映射。我唯一能找到的是一个名为 react-native-smart-pickernpm 包,我可以将其与 .map 一起使用。有局限性。

仅供参考,我还尝试了其他 bootstrap 框架,这是 vanilla react-native 的问题。

这是link.. https://www.npmjs.com/package/react-native-smart-picker

这是我的 github 回购... https://github.com/GavinThomas1192/motoMechanicMeeKanic/blob/master/App/Components/vehicleMakePicker.js

这是我实现它的代码。

<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>

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

<Content>
  <Text>Vehicle Stats:</Text>
  <Text>Year: {this.state.vehicleYear}</Text>
  <Text>Make: {this.state.vehicleMake}</Text>
  <Text>Model: {this.state.vehicleModel}</Text>
  {this.state.vehicleYear === "" ? <VehicleYearPicker vehicleYear={this.yearPicked}/> : undefined}
  {this.state.vehicleYear !== "" && this.state.vehicleMake === "" ?
    <VehicleMakePicker pickedYear={this.state.vehicleYear} vehicleMake={this.makePicked}/> : undefined}
  {this.state.vehicleModel === "" && this.state.vehicleMake !== "" ?
    <VehicleModelPicker homeState={this.state} vehicleModel={this.modelPicked}/> : undefined}
</Content>

我试过你的代码,对我来说工作正常。

粘贴我的代码

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>
    );
  }
}

依赖关系

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