Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React Router: Linking to Route instead of URL? #14

Closed
danburzo opened this issue Jan 12, 2017 · 7 comments
Closed

React Router: Linking to Route instead of URL? #14

danburzo opened this issue Jan 12, 2017 · 7 comments

Comments

@danburzo
Copy link

When using react-router, is there a way to make <Link> components that don't have a hardcoded URL in them? e.g. instead of <Link to='/dashboard/projects'>, have <Link to=' ... projects route... '>. Or, otherwise, have a way to keep the Route definitions in sync with the various Links floating about in the app.

I find it annoying to have to double-check all the Links whenever I shuffle the Route definitions around.

@vladzelinschi
Copy link
Contributor

vladzelinschi commented Jan 12, 2017

Hopefully I'm getting the question right, so please correct me if I misunderstood. You want to be able to change the route string in one place, w/o checking all Link usages in the app. With this idea in mind...

Let's say you have a module in your app:

const ROUTES = {
  PROJECTS: 'dashboard/projects',
  // other here...
};

export default ROUTES;

Then in your other modules you can:

import { Link } from 'react-router';
import Routes from '/path/to/routes/module';

<Link to=`${Routes.PROJECTS}`> // take advantage of template string from ES6 ;)

Is this what you want?

@alexnm
Copy link

alexnm commented Jan 12, 2017

react-router had a name prop that you could add to a <Route />, Docs. There was a discussion when upgrading to 1.0 and they removed it

remix-run/react-router#1840

@vladzelinschi
Copy link
Contributor

As a side advice, I strongly suggest moving to v4. It's just components. Much easier to digest. Versions of RR < 4 have a crappy API. Worse than Nicolas Cage's acting skills.

@alexnm
Copy link

alexnm commented Jan 12, 2017

Totally agree with RR4, but I haven't found a good solution for the way in which you perform server side rendering with prefetching data with RR < 4. I'm investigating it actually these days :)

@danburzo
Copy link
Author

@alexnm the name parameter was exactly what I was looking for, I'll read into the decision to remove it.

@r31gn yes, you understood correctly my intent, and your idea might help make it easier to manage Links. However, I was thinking about something that is, in addition to that, correlated with the Route definitions. Also, I'll give v4 a look, thanks for the tip!

@vladzelinschi
Copy link
Contributor

vladzelinschi commented Jan 12, 2017

@alexnm It's not straightforward. I bumped into that as well. Also, code splitting + lazy loading with Webpack. Every tutorial you'll find make use of System.import, require.ensure and route hooks (onEnter). These no longer exist in RR 4, so the tutorials are not too helpful. I've wanted to investigate and write an article about this, but had little time.

Throw me some resources if you find smth interesting?

@danburzo
Copy link
Author

danburzo commented Jan 12, 2017

Alright, I am closing this as I have been illuminated :) Since URLs tend to change only at development time, we can muster the courage to manage a few dozen Links across the app, without introducing extra complexity into RR 3. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants