Search code examples
react-nativeexpo-router

Getting rest id [...rest].js file from link like VehicleDetails/4


I have a link when clicked takes a user to a specific page. In that page i would like to display specific data from the passed ID. My link looks like VehicleDetails/4

How can i get the id 4

I am using segments

const segmentPattern = /^VehicleDetails\/(\d+)$/; 
const match = segments[0].match(segmentPattern); 
const id = match ? match[1] : null; 

alert(id);

My code returns null.

but i cant get the rest id for instance in VehicleDetails/4


Solution

  • I used pathname and it worked

    onPress={() => { router.push({ pathname: 'VehicleDetails/[id]', params: { id: 4} }); }}

    then

    import { usePathname } from "expo-router";

    const pathName = usePathname();
     const segments = pathName.split('/'); 
     const id = segments[2]; 
     
     alert(id);