Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use absolute path or relative path in React Route

All:

I am pretty new to React Router, when I follow its offical tutorial at lesson 7:

https://github.com/reactjs/react-router-tutorial/tree/master/lessons/07-more-nesting

When it comes to route params like:

// index.js
// ...
<Route path="/repos" component={Repos}>
  <Route path="/repos/:userName/:repoName" component={Repo}/>
</Route>

It starts to use absolute path, I am wondering :

[1] How does React-Router decide to use abosulte path or relative path, is it only because the path starts with slash "/"(one thing I find out is: once I add slash to a relative path at the beginning, that path turns into absolute path, no matter what its parent routes are.)?

[2] Is there a way I can use relative params path route? If not, then what is the point of the parent Route whose path is "/repos"?

Thanks

like image 845
Kuan Avatar asked Jul 06 '16 21:07

Kuan


People also ask

Is absolute path or relative path better?

Absolute URLs must be used to link to other websites that are not located on the same domain. Relative URLs, on the other hand, are more easy to use because they are relative to the page they are on.

Should you use absolute path?

You must use absolute paths when linking to another Website, but you can also use absolute paths within your own website.

Should you use relative paths?

Relative paths rely on the current working directory. This is a form of global state, and as such it should be avoided whenever possible. Depending on the structure of your application, the current working directory may not be guaranteed to be constant through all code paths that can reach a certain routine.

Why do we use absolute paths?

An absolute path makes no assumptions about your current location in relation to the location of the file or directory it's describing. An absolute path always begins from the absolute start of your hard drive and describes every step you must take through the filesystem to end up at the target location.


1 Answers

  1. React router currently uses absolute paths, but they are working on relative routes

  2. If you want relative routes, it looks like people are getting the current route from the match param, and then appending a route to it. E.g., <Route path={match.path + '/more/stuff'}/>

In the example that you gave, the point is that whenever the current path contains /repos there will be some content (the list of links to repos generated by component={Repos}) that will be visible. When the path is /repos/:userName/:repoName it will continue to show that content because the path still matches /repos, but it will also show a particular repo's content (component={Repo}).

like image 130
hellojeffhall Avatar answered Oct 11 '22 08:10

hellojeffhall