React js routing is pretty similar to that of angular. Here, we will be using npm install react-router-dom for routing which will be added to the app.js code. Rest index.js and writing components is written just as basics. This example showcases the app.js page to have 3 links, home, blog 1, blog 2 which will route to 3 individual components. router, route, and switch have been used about which will discuss below. So, let’s get started straight away!
1.npm install react-router-dom
The first step to react js routing is definitely to install the router package. Now, there are other npm modules like react-router for again routing in react js application, but react-router-dom is browser-specific, hence it is more used. Also, reacttraining that manages reactjs development promotes the use of react-router-dom. Hence, that’s what we are going to use here also. So, in the terminal type, the command and it should not take more than a minute to install.
npm install react-router-dom
2.Create components
This sample example of react js routing makes use of 3 components -Home, Blog1, Blog2. We are creating 3 separate files for the same, with the utmost basic code of components creation.
3.Index.js and App.js code
Index.js renders the ReactDom with App.js component attached to it. We haven’t touched this piece of the file for react js routing.
App.js file is the one that would require you to grasp react js routing in detail. First, let’s look at the code.
Explanation:
<Route path=”/blog/100″ component={blog1} />
<Route path={`/blog/:id`} component={blog2} />
</Switch>
4.Run the code and test the output
Run the code using npm start and the application should run fine if the steps are followed correctly, with home being the default output on the screen. This completes your react js routing tutorial.
This was a short tutorial on react js routing. Similar other helpful links are down below, do check them out.
Similar Posts:
from Engineer Diaries https://ift.tt/3coGR50
0 Comments