I am still learning React Native and I am stuck with navigating to a screen with some params. I know that to pass a param to another screen we can do it like this:
/item/[id].jsx
const ItemDetailsScreen = () => {
const { id } = useLocalSearchParams();
return (
<View ...>
...
</View>
);
};
And in the previous screen we just call: router.push(``/item/${id}``)
Now, what if I need to pass two params: id
and name
?
Thank you.
I am still learning React Native and I am stuck with navigating to a screen with some params. I know that to pass a param to another screen we can do it like this:
/item/[id].jsx
const ItemDetailsScreen = () => {
const { id } = useLocalSearchParams();
return (
<View ...>
...
</View>
);
};
And in the previous screen we just call: router.push(``/item/${id}``)
Now, what if I need to pass two params: id
and name
?
Thank you.
Share Improve this question asked Feb 5 at 9:55 Bawenang Rukmoko Pardian PutraBawenang Rukmoko Pardian Putra 1,4211 gold badge19 silver badges40 bronze badges 3 |2 Answers
Reset to default 3You can do this way because you are using Native
import { useNavigation } from '@react-navigation/native';
const Screen1 = () => {
const navigation = useNavigation();
const navigateToScreen2 = () => {
navigation.navigate('Screen2', {
param1: 'value1',
param2: 'value2',
});
};
return (
<View>
<Button title="Go to Screen2" onPress={navigateToScreen2} />
</View>
);
};
You can pass the name in the params
object:
router.push(`/item/${id}`, { params: { name } })
[id][name].jsx
but I guess we can't. And reading the answer below, I think I will just dorouter.push(/item/details, { params: { id, name } })
instead. It just feels wrong to have one param to be handled one way, and another one the other way like in the answer. Thanks guys. – Bawenang Rukmoko Pardian Putra Commented Feb 5 at 13:33