I’m trying to use a selected element from a dropdown in another screen, but when I access the other screen where I expect the selected element to be rendered, it gives me undefined:
This is the screen (Screen1) where I select the item:
<TouchableOpacity
onPress={() => {
navigation.navigate('Screen2', { fieldName: selectedField.name });
}}
>
I want after clicking to navigate to the other screen and display the selectedField.name value there.
Here is the other screen – Screen2:
const Screen2 = ({ route }) => {
const navigation = useNavigation();
const { params } = route;
const fieldName = params.fieldName;
console.log('Params:', params);
return (
<View>
<Text style={{ fontSize: 24, fontWeight: 'bold' }}>
Selected field: {fieldName}
</Text>
<Button onPress={() => navigation.goBack()} title="Go back" color="#662044" />
</View>
);
};
I get the following error: "TypeError: Cannot read property ‘fieldName’ of undefined"
In Screen1 through console log I get the value of the selected field successfully, but in Screen2 it doesn’t work – gives me that the parameter is undefined
I can’t use an item in another screen in React Native
2
Answers
You can achieve this by updating your
Screen2
like below:Use
useRoute
hook instead of passed parameter, here is an example