Working with React-Router? – User friendly Tech help

“An investment in knowledge pays the best interest.” Benjamin Franklin
n
nIn our previous post we started with creating our first react app and than we learned about some basic terminologies in react. Today we are going to focus on creating the router behaviour using react library.
n
nIncase you want to learn about filtering in react check the github code. n

nPre-requites:- 
nGithub for React Router
n

n

npm install --save react-router-dom

n

Key Terms:-
n
n1) BrowserRouter, this component listens to changes in URL and display the correct screen on UI
n

n

    n

  • Import Browser router in src/index.js  (Line 6 in the below code example)
  • n

  • Enclose component inside (Line 8)
  • n

n

n

n

n

n

n

n

 1 2 3 4 5 6 7 8 9
10
n

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
import './index.css';
import { BrowserRouter } from 'react-router-dom'
ReactDOM.render(,
document.getElementById('root'));registerServiceWorker();

n

n2) Link, this component accepts the to property which directs the app to the given path. 
n

n

n

n

n

n

n

n

123
4
n

 Add Contact

n

>ContactLink component fully renders a proper anchor tag (a) with the appropriate href, you can expect it to behave how a normal link on the web behaves.
n
n3) Route Component, this component is a critical piece of building an application with React Router because it’s the component which is going to decide which components are rendered based on the current URL path.

n

n

n

n

n

n

n

12
3
n

 

n

Working Code with Router

Was this article helpful?
YesNo

Similar Posts