react-native-picker / picker

Picker is a cross-platform UI component for selecting an item from a list of options.
MIT License
1.49k stars 280 forks source link

Invalid hook call when using Picker on Android #424

Open MandoMG opened 2 years ago

MandoMG commented 2 years ago

I implemented the picker on iOS but on Android I'm getting an Invalid hook call everytime I reach the component where my picker lives. Is anyone else seeing this issue?

image
const ItemPicker = (props: ItemPickerProps) => {
 return (
    <View>
      <Picker
          selectedValue={selectedItem}
          onValueChange={(itemValue, itemIndex) =>
            setSelectedItem(itemValue)
          }>
          <Picker.Item label="Java" value="java" />
          <Picker.Item label="JavaScript" value="js" />
        </Picker>
    </View>
  )
};

export default ItemPicker;

"react": "17.0.2", "react-native": "0.67.1", "@react-native-picker/picker": "^2.4.1"

erez-unitronics commented 2 years ago

+1 "react": "18.0.0", "react-native": "0.69.3" "@react-native-picker/picker": "^2.4.2"

Nesh108 commented 2 years ago

Same here!

"react": "18.2.0",
"react-native": "0.69.5",
"@react-native-picker/picker": "^1.8.3",
frabanca commented 1 year ago

As reported by @stocaaro here, it seems to be an issue related to ReactNative >= 0.69.

There are 3 workarounds:

  1. Use --legacy-peer-deps when installing picker npm install @react-native-picker/picker --legacy-peer-deps (worked for me)
  2. Use an older version of react-native npx react-native@0.68.0 init $PROJECTNAME --version 0.68.0
  3. Use a package manager that doesn't enforce peer dependencies such as yarn or npm prior to 7
farmstrong8 commented 1 year ago

bump

nlarif commented 1 year ago

Same here with :

"react": "18.0.0"
"react-native": "0.69.6"
 "@react-native-picker/picker": "2.4.3"
nlarif commented 1 year ago

Solved for me. I am using react-native-picker-select on top of @react-native-picker/picker. I found out that react-native-picker-select was installing on old version of @react-native-picker/picker under node_modules/react-native-picker-select/node_modules/@react-native-picker/picker which was the one throwing the exception. I updated my packages.json with:

"resolutions": {
    "react-native-picker-select/@react-native-picker/picker": "^2.4.8"
  },

and used Yarn locally and in my CI/CD. I removed the usage of npm install --save --legacy-peer-deps which is no longer needed. Now react-native-picker-select is relying on the newest version within node_modules and I don't see the error anymore.