简体   繁体   中英

How do get params that have no specific names

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 eg

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}`}>

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 ie

<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()

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}>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM