Ali Zeynalov
Ali Zeynalov

Reputation: 3017

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

I'm using Picker component of Native-Base for my react-native application. On IOS everything is ok, whereas, on Android side I can not trigger function I added on onValueChange.

Is there anyone faced this issue before?

How did you fix it? I stuck here almost a day.

Here is my code.

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

It does not call the function onBranchSelected on Android.

Upvotes: 2

Views: 8487

Answers (2)

Gavin Thomas
Gavin Thomas

Reputation: 1867

This is known issue with Picker. The issue is trying to use .map. I myself couldn't ever get map to work with Picker. The only thing I could find was an npm package called react-native-smart-picker which I was able to use a .map with. There are limitations.

And FYI I also tried other bootstrap frameworks and this is an issue with vanilla react-native.

Heres the link.. https://www.npmjs.com/package/react-native-smart-picker

Heres my github repo... https://github.com/GavinThomas1192/motoMechanicMeeKanic/blob/master/App/Components/vehicleMakePicker.js

Heres my code where I implemented it.

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

Update to show how I handled no expandable button

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

enter image description here

enter image description here

Upvotes: 1

akhil xavier
akhil xavier

Reputation: 1847

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

Upvotes: 3

Related Questions