You can use React Router library for routing your pages. For example first create router for your pages like this :
In your root file, or create another file called "Router.js" and render this, then you can render <Router/>
component in your root file.
import React from "react";
import {
BrowserRouter as Router,
Switch,
Route,
Link,
useHistory
} from "react-router-dom";
const BasicExample = () => {
return (
<Router>
<Switch>
<Route exact path="/">
<Home />
</Route>
<Route path="/about">
<About />
</Route>
</Switch>
</Router>
);
}
export default BasicExample;
In a file from where you want to navigate, for example Home.js :
const Home = () => {
const history = useHistory();
const clickMe = (data) => {
history.push("/about", {data: data});
}
return (
<div>
<h2>Home</h2>
<button onClick={() => clickMe({name: "test"})} className="ViewDetailsBtnLink">View Details</button>
</div>
);
}
export default Home;
In a file where you want to access passed parameter. Here is About.js :
const About = () => {
const history = useHistory();
const data = history.location.state.data;
return (
<div>
<h2>About</h2>
<p>{JSON.stringify(data)}</p>
</div>
);
}
export default About;
As you can see, you can use useHistory hook to navigate to another page with parameter and to access the parameter passed from previous page.