My Next version is 13.1.5. I am trying to send contact.name
through the Link
in my component. Whats the best way to do this? The first code of the Link
is just my original code.
<Link
className={styles.summaryLink}
href={`${routes.accountShow(contact.number)}/contacts`}
>
{contact.name}
</Link>
This second is what I tried to do with pathname
and query
but when I do this it keeps giving an error saying href
needs to be a string
and the url also goes to /contacts/[object,object]
. I am also not trying to show contact.name
in the url at all if possible.
<Link
className={styles.summaryLink}
href={{
pathname: `${routes.accountShow(contact.number)}/contacts`,
query: { contactName: contact.name },
}}
>
{contact.name}
</Link>
2
Answers
You might find a context helpful at this point.A React Context allows defined data to be shared across components.
In your case you could implement it as follows
in your root component you must then create the context provider
at this point we have defined the state contactName and pass the value and the set function to the created context.
Each component can now set the state of the MyApp component.
in the area of your navigation, you would then have to set the context accordingly. You can then access the corresponding function as well as the value itself via the useContext hook
The display component can also read the values via the useContext hook and then display the data accordingly.
but a little warning. If you use a lot of context based components, code becomes very quickly bad maintainable. I would give you the idea to replace the name with a kind of ID and define it as part of the route.
The first one is correct.
But you should use
to
attribute instead ofhref
.Let me share the updated code