L.Calvin
L.Calvin

Reputation: 251

How do I make use of the Drop Down Picker in react native expo?

My code:

import { TouchableOpacity, StyleSheet, Text, View } from 'react-native'
import React from 'react'
import { auth } from '../firebase'
import { useNavigation } from '@react-navigation/core'
import { signOut } from 'firebase/auth'
import DropDownPicker from 'react-native-dropdown-picker'

const HomeScreen = () => {

  const navigation = useNavigation()
  const handleSignOut = async () =>{
    try{
      await signOut(auth)
      console.log("Signed out successfully")
      navigation.replace("Login")
    }catch (error) {
      console.log({error});
   }
  }
  return (
    <View style = {styles.container}>
      <Text>Welcome {auth.currentUser?.email}</Text>
      <Text></Text>
      <Text>What cusine would you like to eat today?</Text>
      <DropDownPicker
          items={[
              {label: 'English', value: 'en'},
              {label: 'Deutsch', value: 'de'},
              {label: 'French', value: 'fr'},
          ]}
          defaultIndex={0}
          containerStyle={{height: 40}}
          onChangeItem={item => console.log(item.label, item.value)}
      />
      <TouchableOpacity
      onPress={handleSignOut}
        style = {styles.button}
      >
        <Text style = {styles.buttonText}>Sign Out</Text>
      </TouchableOpacity>
    </View>
    
  )
}

I am using https://snack.expo.dev/a6rDxpeU_ as reference. The drop down picker is suppose to look like: enter image description here

But I got nothing on my screen though. There is no error or warning from the expo app or my terminal My screen looks like: enter image description here

Or am I doing something wrong here? I have done the import which is import DropDownPicker from 'react-native-dropdown-picker' and before the import, I have done the installation in my root folder which is npm install react-native-dropdown-picker from https://hossein-zare.github.io/react-native-dropdown-picker-website/docs. I also checked that under my package.json, I have the following dependencies: enter image description here

I have tried to pass the data that is selected in the drop down picker via https://www.youtube.com/watch?v=C01CJlu7Q-I

import { TouchableOpacity, StyleSheet, Text, View } from 'react-native'
import React from 'react'
import { auth } from '../firebase'
import { useNavigation } from '@react-navigation/core'
import { signOut } from 'firebase/auth'
import DropDownPicker from 'react-native-dropdown-picker'
import { useEffect, useState } from 'react'

const HomeScreen = () => {

  const navigation = useNavigation()

  const [open, setOpen] = useState(false);
  const [value, setValue] = useState(null);
  const [items, setItems] = useState([
             {label: 'Japanese', value: 'J'},                  
             {label: 'Korean', value: 'K'},
             {label: 'Western', value: 'F'},
             {label:'Indonesian', value:'I'},
             {label: 'Taiwan', value: 'T'},
             {label:'Chinese', value:'C'},
            ]);
  const handleSignOut = async () =>{
    try{
      await signOut(auth)
      console.log("Signed out successfully")
      navigation.replace("Login")
    }catch (error) {
      console.log({error});
   }
  }
  
  return (
    <View style = {styles.container}>
      <Text>Welcome {auth.currentUser?.email}</Text>
      <Text></Text>
      <Text>What cusine would you like to eat today?</Text>
      <DropDownPicker
      open={open}
      value={value}
      items={items}
      setOpen={setOpen}
      setValue={setValue}
      setItems={setItems}
    />
    <TouchableOpacity
        onPress={() => navigation.navigate("SubScreen1", {paramKey:items})}
        style = {styles.button}
      >
        <Text style = {styles.buttonText}>Search</Text>
      </TouchableOpacity>

      <TouchableOpacity
      onPress={handleSignOut}
        style = {styles.button}
      >
        <Text style = {styles.buttonText}>Sign Out</Text>
      </TouchableOpacity>
    </View>
    
  )
}

But when i clicked the search button, it is not jumping to the next page. I assume its something wrong with the passing of the key. How do i fix this?

Upvotes: 1

Views: 3633

Answers (1)

R. Mahbub
R. Mahbub

Reputation: 432

Try it plz. Hope it would work.

import DropDownPicker from 'react-native-dropdown-picker';
function HomeScreen() {
  const [open, setOpen] = useState(false);
  const [value, setValue] = useState(null);
  const [items, setItems] = useState([
             {label: 'English', value: 'en'},                  
             {label: 'Deutsch', value: 'de'},
             {label: 'French', value: 'fr'},
               ]);

  return (
    <DropDownPicker
      open={open}
      value={value}
      items={items}
      setOpen={setOpen}
      setValue={setValue}
      setItems={setItems}
    />
  );
}

You may found this useful: React native: Dropdown-link

Upvotes: 1

Related Questions