Reputation: 1949
What is the difference between Link and Route in react router? Where do we use them or do we use them together or separately? Where can I find genuine and professional React web development tutorial?
Upvotes: 16
Views: 21525
Reputation: 3413
The Route
and Link
components are meant to be used together to accomplish certain tasks related to the URL of an application. Firstly, the React Router library is intended to achieve two things: 1. Ensures that state transitions are captured in the URL bar. 2. Ensures that the application starts from a proper state according to a stateful URL address.
Role of Link Component
The Link component is a way to transition the route state in the application. So if you click a Link
component, a route state will be activated changing the URL path accordingly. For example:
<Link to="/example" />
will register that the URL path of the application is at '/example'.
Role of Route Component
At this point, it is up to the Route
component to render the appropriate UI content according to the URL as such:
<Route path="/example" component={Example} />
So in a nutshell, the Link
component is responsible for the transition of the route (page to page), while the Route
component acts as a switch to display different components based on the route state.
The best example of the router library usage can be found at React Training.
Upvotes: 20
Reputation: 31
In Layman's terms we could understand the difference as mentioned below:
a)Routes are responsible for rendering UI when its path matches current URL where as Links are similar to anchor tags in HTML which are used to access the route paths.
b) Routes involve components within it where as Links involve just the path which is already defined by the routes
c) Routes are independent entities where as Links are dependent on Routes.
Upvotes: 3
Reputation: 758
A less tehcnical explanation:
<Route exact path="/articles"> <Articles /> </Route>
means that "/articles" route is created and you do not have to create it again in other components. That's why generally Route goes inside the main component, which is generally "App".
Upvotes: 1
Reputation: 93
I will explain by an Example, If I have:
<Router>
<Route path = "/" component = {LoginModule} />
<Route path = "/Register" component = {RegisterModule}/>
</Router>
Here if i go to URL localhost:3000
, it will render the Login Module. No doubt in that.
But if I go to URL localhost:3000/Register
, it will render both login and register module below the login module on the same page. It is because /Register also has '/' in it.
This can be solved by:
<Router>
<Route exact = {true} path = "/" component = {LoginModule} />
<Route path = "/Register" component = {RegisterModule}/>
</Router>
Upvotes: 2
Reputation: 215
<Link/>
is the element you could use to navigate through routes.
<Route/>
, its most basic responsibility is to render some UI when a location matches the route’s path.
They are used separately. Link is dependent to Route's locations. But Route can be used without Link.
React Router: https://reacttraining.com/react-router/web/guides/philosophy
React JS Tutorial: https://reactjs.org/docs/hello-world.html
Upvotes: 8