2 April 2020

Router example in react js

  • Create Components folder under src folder
  • Add RouterEXP1.js file and add below Code
import React, { Component } from "react";
import { BrowserRouter as Router, Route, Link } from "react-router-dom";

import LoginEXP from "./LoginEXP";
import FindDOMNode from "./FindDOMNode";
import PopsDefaultInFunEXP1 from "./PopsDefaultInFunEXP1";
class RouterEXP1 extends Component {
render() {
return (
<div>
<Router>
<div>
<ul>
<li>
<Link to="/">LoginEXP</Link>
</li>
<li>
<Link to="/FindDOMNode">FindDOMNode</Link>
</li>
<li>
<Link to="/PopsDefaultInFunEXP1">PopsDefaultInFunEXP1</Link>
</li>
</ul>

<hr />

<Route exact path="/" component={LoginEXP} />
<Route path="/FindDOMNode" component={FindDOMNode} />
<Route
path="/PopsDefaultInFunEXP1"
component={PopsDefaultInFunEXP1}
/>
</div>
</Router>
;
</div>
);
}
}
export default RouterEXP1;
  • Open App.js file and update code as below\
import React from "react";
import ReactDOM from "react-dom";
import RouterEXP1 from "./components/RouterEXP1";

function App() {
return (
<div className="App">
<RouterEXP1 />
</div>
);
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Output:





No comments:

Post a Comment