reactjs / react-router-tutorial

5.52k stars 1.75k forks source link

How to pass params by Link ? #327

Open eromoe opened 7 years ago

eromoe commented 7 years ago

Hi,

I know I can pass params by Route like

<Route path="/corpus/:corpusId/tag" component={TagPage} tags={tags}  />}/>

But that require set component , and component must be child of current page. That is inconvenient.

I'd like to use Link like

<Link path="/corpus/:corpusId/tag" tags={tags}  />}/>

Automatically pass tags to certain component.

luish commented 6 years ago

You can pass an object to the Link:

<Link to={{
  pathname: '/courses',
  search: '?sort=name',
  hash: '#the-hash',
  state: { fromDashboard: true }
}}/>

https://github.com/ReactTraining/react-router/blob/dc10832d23f9ab673891805c9c866b90d1ef1ed6/packages/react-router-dom/docs/api/Link.md#to-object

MiniRaccoonDog commented 5 years ago

Will this work for you?

<Link to={/corpus/${corpusId}/tag}>

Assuming of course the "corpusId" you wanted exists already by the time you make that Link

Well that is just ruining my formatting. basically there are backticks ` around the "/corpus.../tag"

fonziemedia commented 5 years ago

Why are people down voting @luish 's answer?

That's the correct answer according to the docs..

MiniRaccoonDog commented 5 years ago

Why are people down voting @luish 's answer?

That's the correct answer according to the docs..

I.. think its because its straight copy-pasted from the react-router docs and isn't tailored to the asker's original question? Which isn't as ideal because, in theory the asker has already seen those docs and didn't quite follow how to get the iteration they desired. People are looking for a little more than C&P, they want a tailored response

luish commented 5 years ago

Well, my answer replied to the question in the title "How to pass params by Link?" Anyway this is an old issue that hopefully got resolved. 🙂

GOFFARTSylvain commented 5 years ago

I would like to know if there's a solution to past params "invisibles" in the query or if the params HAVE to be visibles ?