Home > Back-end >  How do get params that have no specific names
How do get params that have no specific names

Time:10-02

I have been trying to find how to get multiple params in react and in most of the examples the params have names like :id e.g

to="/movie/:id"

So to get such you just use:

const {id} = useParams();

But for my case i don't have specific names. How to i get these params?

//res is an object
<Link to={`/movie/${res.id}/${res.link_id}`}>

CodePudding user response:

You have to assign the component in the Link props: Suppose that your new Component is named MovieDetails.

const MovieDetails=()=>{
    const {id} = useParams();
}

Link Component:

<Link to={`/movie/${res.id}/${res.link_id}`} component={MovieDetails}>

CodePudding user response:

I have figured it out. I confused <Route> with <Link>

In the <Link> tag i don't need to define the params. It is the <Route> tag that the params are already defined i.e

<Route path='/movie/:movieId/:linkId' element={<Movie />} />

//Then in link tag is just an like an <a> tag
<Link to={`/movie/${res.id}/${res.link_id}`}>

Then in my component I just get them as

const {movieId,linkId} = useParams()
  • Related