Pass object through Link in react router
Asked Answered
N

7

45

Is it possible to pass an object via Link component in react-router?

Something like:
<Link to='home' params={{myObj: obj}}> Click </Link>

In the same way as I would pass props from the Parent to Child component.

If it's not possible what is the best way of achieving this:
I have a React + Flux app, and I render the table with some data. What I am trying to do is when I click on one of the rows it would take me to some details component for this row. The row has all of the data I need so I thought it would be great if I could just pass it through Link.

The other option would be to pass the id of the row in the url, read it in the details component and request the data from the store for by ID.

Not sure what is the best way of achieving the above...

Nicollenicolson answered 1/7, 2015 at 17:32 Comment(1)
Depending on how you fetch your data overall, I would say the latter option.Doretheadoretta
N
42

So my final conclusion on this question is that I didn't think it through properly. It seemed natural just to pass my data through the Link so I can access them in my Child component. As Colin Ramsay mentioned there is something called state in the Link but that's not the way to do it. It would work fine when the data is passed through Link only if the user clicks on something and is taken to the Child component.

The problem comes when the user accesses the url which is used in Link then there is no way to get the data.

So the solution in my case was to pass the ID in Link params and then check if my Store has the data (user accesses it via Link) and then getting this data from the Store.

Or if the data is not in the Store call the action to fetch the data from the API.

Nicollenicolson answered 4/7, 2015 at 9:59 Comment(2)
In react-router 3.0.0 the Link component doesn't have the "params" attribute. How is it done for the current version ?Pinnatisect
What is a proper use case for the "state" property in Link if it won't work when the URL is accessed directly?Arran
P
15

It is possible to pass an object through a Link. (it's not best practice, but it's possible)

Add your data within a query rather than params and stringify the object:

<Link to={{ 
  pathname: `/blog/${post.id}`, 
  query: {
    title: post.title, 
    content: post.content,
    comments: JSON.stringify(post.comments)
  } 
}}>Read More...</Link>

Then in your child component parse the string back in to an object:

JSON.parse(this.props.comments)
Pulpwood answered 11/1, 2018 at 1:43 Comment(5)
but you need to refer to the react router official documentation to see how to pass object ot to prop on link , it is the same for NavLink no difference here , yours are a little bit off of that but any way , upvoted... , but please update your answer for avoiding the future misguides ....Jiva
@a_m_dev thanks could expand on what you said? maybe posting your own answer based on what you saw on the docs would help future readers. looking at these docs reacttraining.com/react-router/web/api/NavLinkPulpwood
@Pixelomo, How is the query object accessible in the component that the route renders?Messer
@Messer see the end of my answer JSON.parse(this.props.comments)Pulpwood
Try finding it under this.props.location.query.title . And you will be able to access the value.Midshipmite
D
12

No, you can't pass an object in params, so I would agree with you that your best plan is to pass the id to a store, have the store emit a CHANGE event, and have components query the store for info.

Dickerson answered 1/7, 2015 at 23:38 Comment(0)
T
3

This isn't possible, you need to pass something that can be stored in the URL such as a string ID. You would then use that ID to perform a lookup of the object.

Truscott answered 3/7, 2015 at 16:12 Comment(1)
The only alternative would be to pass the object as JSON but either way you would still be storing it in a form that gets stored in the URL. There's also a state prop for Link that you could look at but it's not the same thing.Truscott
R
1

You could try JSON serializing it and then de-serializing it on the receiving end.

Reisinger answered 17/12, 2015 at 22:1 Comment(0)
A
0

Using react-roter-dom ver 5.1.2 and es6

I solved the problem like this first I store it in the search part of Link to object meaning it is part of the url and supports a bookmark etc.

< Link to={{
          pathname: `${match.url}/${dogName}`,
          search: `choosenDog=${JSON.stringify({ ...dog })}` //dog is the object to pass along
        }
        } >

And then on the link we goto I get hold of it like this

const query = useQuery();
const dogString = query.get('choosenDog');
const dogObject  = JSON.parse(dogString);

Audette answered 28/10, 2020 at 17:2 Comment(0)
Z
0

Yes it is possible to pass object via Link component in react. All you have to do is to pass the object to state property of the component

<Link to='home' state={{myObj: obj}}> Click </Link>

Then after that you can access the object using useLocation() hook. Note that the object should be access in the path as defined in the Link to perperty

const {state} = useLocation();
console.log(state); // {myOgj: obj}
Zelazny answered 15/12, 2023 at 11:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.