Problem:
I am very new to react stuff. I am creating a web application. In there I have implemented a way scroll to top like this way.
import React, { PureComponent } from "react";
import "../../../assets/css/bootstrap.min.css";
import "../../../assets/css/demo.css";
import "../../../assets/css/light-bootstrap-dashboard.css";
import "../../../assets/css/font-awesome-min.css";
import "../../../assets/css/fonts.css";
import "../../../assets/css/dashbord.css";
import Sidebar from "../Templates/Sidebar";
import Header from "../Templates/Header";
import Footer from "../Templates/Footer";
import Dashbordcards from "./Dashbordcards";
import Graphs from "./Graphs/Graphs";
import ActivityFeed from "./ActivityFeed";
import { Row, Col, Button } from "react-bootstrap";
class Dashboard extends PureComponent {
componentDidMount() {
window.addEventListener("scroll", this.handeleScroll());
}
handeleScroll() {
if (
document.body.scrollTop > 20 ||
document.documentElement.scrollTop > 20
) {
document.getElementById("myBtn").style.display = "block";
} else {
document.getElementById("myBtn").style.display = "none";
}
}
topFunction(){
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}
render() {
return (
<div className="wrapper">
<Button onclick={this.topFunction} id="myBtn" title="Go to top">
Top
</Button>
<div className="sidebar-background">
<Sidebar />
</div>
<div className="main-panel">
<Header name="Dashbord" />
<div class="content">
<Dashbordcards />
<Graphs />
<ActivityFeed />
</div>
<Footer />
</div>
</div>
);
}
}
export default Dashboard;
But it is not working as expected. I tried so many examples in the stack overflow and the examples that I found through google searches. But those were not able to full fill my requirement. Can someone help me to solve this problem by modifying my code and make it workable? Thank you very much!