Answer the question
In order to leave comments, you need to log in
How to navigate to another page on button click in React?
Hello! Help me please!
The thing is that I'm trying to go to a completely new page of my project in React by clicking on a button. Then, on that other page, there's a 'Back' button that takes you back. Below is one of my attempts to implement the first step (In the address bar, when you click on the button, the link is displayed, but the page does not appear). If you have any ideas what I'm doing wrong, please share)
import React from "react";
import ReactDOM from "react-dom";
import Statistics from "../Statistics_components/Statistics.js";
class Statistics_button extends React.Component {
onclick () {
window.location.assign('../Statistics_components/Statistics.js');
}
render() {
return (<button onClick={(e) => this.onclick(e)}><i className="fas fa-chart-bar"></i></button>);
}
}
export default Statistics_button;
Answer the question
In order to leave comments, you need to log in
Friends, finally the clarification has come. Below is an example implementation:
Import your pages into the main App component, in this case it is FirstPageComponent and SecondPageComponent
import React, { Component } from "react";
import FirstPageComponent from "./FirstPageComponent.js";
import SecondPageComponent from"./SecondPageComponent";
import { BrowserRouter as Router, Route, Link } from "react-router-dom";
function App (){
return(
<Router>
<div className="App">
<Route path="/" exact exact component={FirstPageComponent}
}/>
<Route path="/second" exact component={SecondPageComponent}/>
</div>
</Router>
)
}
export default App;
import React from "react";
import ReactDOM from "react-dom";
class OnSecondPageButton extends React.Component {
onclick () {
window.location.assign('http://localhost:3000/second/');
}
render() {
return (<a onClick={(e) => this.onclick(e)}><i className="fas fa-chart-bar"></i></a>);
}
}
export default Statistics_button;
import React from "react";
import ReactDOM from "react-dom";
class Back_button extends React.Component {
onclick () {
window.location.assign('http://localhost:3000/');
}
render() {
return (<button onClick={(e) => this.onclick(e)}>Back </button>);
}
}
export default Back_button;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question