Passing additional params/data to routes? #3407
-
Hey all! I'm new to Remix/React router thus the confusion for me... My case is as follow: I have a list of items like this: {
"data": [
{
"id": 1,
"internal_id": 5487,
"name": "some name 1"
},
{
"id": 2,
"internal_id": 7158,
"name": "some name 2"
},
{
"id": 1,
"internal_id": 58963,
"name": "some name 3"
}
]
} I am loading this data in a page/route, lets say "/", where I build some links to each entry, using the following file structure inside my "/app/routes/$internal_id/$name.js". In "$name.js" I want to query data by "id" instead of "internal_id" key. How do I pass additional/arbitrary params or data to routes? Basically, I want my routes to look like: https://somehost.com/:internal_id/:name but when some user is calling that route, I want to query loader by "id". Hope it makes sense :) Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Since loaders are independent, you will either need to pass the actual Option 1// list.tsx
<Link to={`${internal_id}${name}?id={id}`>${name}</Link>
// $name.tsx
export const loader: LoaderFunction = async ({request}) => {
// url = /some_internal_id/some_name?id=some_id
const url = new URL(request.url)
const id = url.searchParams.get('id')
const data = await queryById(id)
} Option 2// list.tsx
<Link to={`${internal_id}${name}`>${name}</Link>
// $name.tsx
export const loader: LoaderFunction = async ({request, params}) => {
// url = /some_internal_id/some_name
const { internal_id } = params
const item = await queryByInternalId(internal_id)
const data = await queryById(item.id)
} |
Beta Was this translation helpful? Give feedback.
Since loaders are independent, you will either need to pass the actual
id
to your$name
route (via search params), or query data by internal id, to get the id, then query by id.Option 1
Option 2