Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Prevent 'TypeError: Cannot read property 'value' of undefined' in Picker Component #605

Merged

Conversation

aahmadsaleem95
Copy link
Contributor

While working on a simple task in my React Native Expo application, I encountered an issue when using a picker component. The error was observed on my Android device during runtime.

Problem
The error message suggested a potential issue with null or undefined values being passed to the picker. After investigating further, I discovered that the value prop for some Picker.Item components was not being validated properly in this case if an empty string was passed. Causing the application to crash in specific scenarios.

Code Snippet

<View style={styles.dropdownContainer}>
     <Text style={styles.label}>City / Region</Text>
      <Picker
            selectedValue={city}
            style={styles.picker}
            onValueChange={(itemValue) => {
              setCity(itemValue);
              setArea("");
            }}
          >
          <Picker.Item label="Select City" value="" />
          <Picker.Item label="Lahore" value="Lahore" />
          <Picker.Item label="Faisalabad" value="Faisalabad" />
      </Picker>
 </View>

Solution
To address this, I implemented null checks in the code to ensure that value prop is validated properly. I thoroughly tested the fix locally and confirmed that the issue no longer occurs on Andriod devices.

94a33faf-e29d-4351-aa29-9952bb05b46a

Copy link
Contributor

@Naturalclar Naturalclar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@Naturalclar Naturalclar merged commit 5e08652 into react-native-picker:master Dec 2, 2024
4 of 12 checks passed
@Naturalclar
Copy link
Contributor

🎉 This PR is included in version 2.10.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants