Ok very simple example with react router.
1
npx create-react-app testrq-router && cd $_ && yarn add react-router-dom
Modify index.js to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import { Header, Homepage, Aboutpage } from './App';
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<Router>
<Header/>
<Routes>
<Route path="/" element={<Homepage />} />
<Route path="/about" element={<Aboutpage />} />
</Routes>
</Router>
);
Modify App.js to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import logo from './logo.svg';
import './App.css';
import { Link } from 'react-router-dom';
const Header = () => {
return (
<div>
<p>Header</p>
</div>
)
};
const Homepage = () => {
return (
<div>
<h1>Homepage </h1>
<Link to='/about'>Go to Aboutpage</Link>
</div>
)
};
const Aboutpage = () => {
return (
<div>
<h1>Aboutpage<\/h1>
<Link to='/'>Go to Homepage</Link>
</div>
)
};
export {Header, Homepage, Aboutpage } ;
base on understanding-links-in-reactjs mod to react-route v6
Header Component already placed as not rerender component.