#Get URL using UseLocation
1 messages · Page 1 of 1 (latest)
Are you using dynamic routes?
Like app/routes/$slug?
If so, you can create a link with the component name as the slug, and use params to get the slug value.
export const loader = async ({ params: { slug } }) => {
return slug;
};
Here's an example - https://stackblitz.com/edit/github-mn8uqj?file=app/routes/$slug.jsx
Actually the slug doesn't have to be the component name, it can be anything.
// app/routes/index.jsx
export default function Index() {
return (
<div>
<h1>Welcome to Remix</h1>
<ul>
<li>
<Link to="/987sd098s7dg098sa7g8df0g9">Some Component</Link>
</li>
<li>
<Link to="/09g7df90g87df06g987dg089d">Another Component</Link>
</li>
<li>
<Link to="/85fd57fg7g5sd87g65s798gf6">Different Component</Link>
</li>
</ul>
</div>
);
}
// app/routes/$slug.jsx
export const loader = ({ params: { slug } }) => {
return slug;
};
export default function () {
const slug = useLoaderData();
const choice = useRef();
switch (slug) {
case '987sd098s7dg098sa7g8df0g9':
choice.current = <SomeComponent />;
break;
case '09g7df90g87df06g987dg089d':
choice.current = <AnotherComponent />;
break;
case '85fd57fg7g5sd87g65s798gf6':
choice.current = <DifferentComponent />;
break;
default:
break;
}
return choice.current;
}
you can use useParams on the client to get the slug. you don't have to send it down from the loader
However! I will say that what you're doing sounds a lot like routing. if there are particular things you want to render at particular slugs, just make route files for them! you can have both particular slugs and a wildcard $slug — the particular ones will match when they're supposed to, and then the wildcard will work as a fallback
if you're looking at the location at click time, it's going to show the current path, not the next path. the answer is to use routing — handle this on the page you land on, not the page where the link is
Hey crespo, yes. l am looking for the next page. What's the best suggestion then?
could you share some of your code so I can make sure I understand what you’re trying to do?
Good to know. Thanks!
depending on what you're trying to do, if you're looking at the path to decide what to render, that is best done with normal routes instead of if-else-if-else with useLocation().pathname. but I'd like to see what part of the path you're looking at. if the location you're seeing is the old one, that probably means you're looking at it too early
app/routes/
├── current-page.tsx
└── users/
├── special-user1.tsx
├── special-user2.tsx
└── $id.tsx
Apparently I can just use useTransition to get the next step l'm navigating to ❤️
Inside this value I can get the pathname
I'm curious why you want to know that before the transition is complete
I am using placeholder loaders to show the user a "loading page" before the data is loaded. But I have different pages and I need to show different placeholders for that. All l needed to check is which page is going to be loaded to choose the correct form of placeholders
interesting! I see. and because the next page doesn't render until the loader is complete, you can't handle the loading state on the next page