How to Create a Scroll To Top Button in React JS ?
Last Updated :
09 Jan, 2025
You will see there are lots of websites, that are using a useful feature like if you're scrolling the webpage, and now you are at the bottom of that page then you can use this button to scroll up automatically like skip to the content. The following example covers create a Scroll To Top button in React JS using useState() hook.
Prerequisite:
Approach
To create a scroll to top button in react we will use the JavaScript scrollTo method. Define a scroll button and toggle its visibility at some scroll point. Define a function scroll to top using link it to click event in scroll to top button. We will use styled-components for styling the button and the components.
Steps to SetUp React App and Install Required Modules
Step 1: Initialize React Project
You will start a new project using create-react-app so open your terminal and type.
npx create-react-app react-scroll-top
Step 2: Switch to the Project Directory
Now go to your react-scroll-top folder by typing the given command in the terminal.
cd react-scroll-top
Step 3: Install Required module
Install the dependencies required in this project by typing the given command in the terminal.
npm install --save styled-components
npm install --save react-icons
Now create the components folder in src then go to the components folder and create two files ScrollButton.js and Styles.js.
Project Structure:
The file structure in the project will look like this.

The updated dependencies in the package.json file are:
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-icons": "^5.3.0",
"react-scripts": "5.0.1",
"styled-components": "^6.1.13",
"web-vitals": "^2.1.4"
},
Example: This example demonstrates how to create scroll to top button with the help of scrollTo method and useState to toggle the button visibility.
JavaScript
// Filename - App.js
import { Fragment } from "react";
import ScrollButton from "./components/ScrollButton";
import { Content, Heading } from "./components/Styles";
function App() {
return (
<Fragment>
<Heading>GeeksForGeeks</Heading>
<Content />
<ScrollButton />
</Fragment>
);
}
export default App;
JavaScript
// components/ScrollButton.js
import React, { useState } from "react";
import { FaArrowCircleUp } from "react-icons/fa";
import { Button } from "./Styles";
const ScrollButton = () => {
const [visible, setVisible] = useState(false);
const toggleVisible = () => {
const scrolled = document.documentElement.scrollTop;
if (scrolled > 300) {
setVisible(true);
} else if (scrolled <= 300) {
setVisible(false);
}
};
const scrollToTop = () => {
window.scrollTo({
top: 0,
behavior: "smooth"
/* you can also use 'auto' behaviour
in place of 'smooth' */
});
};
window.addEventListener("scroll", toggleVisible);
return (
<Button>
<FaArrowCircleUp
onClick={scrollToTop}
style={{ display: visible ? "inline" : "none" }}
/>
</Button>
);
};
export default ScrollButton;
JavaScript
// components/Styles.js
import styled from "styled-components";
export const Heading = styled.h1`
text-align: center;
color: green;
`;
export const Content = styled.div`
overflowy: scroll;
height: 2500px;
`;
export const Button = styled.div`
position: fixed;
width: 100%;
left: 50%;
bottom: 40px;
height: 20px;
font-size: 3rem;
z-index: 1;
cursor: pointer;
color: green;
`;
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output.
- Using default behavior (auto): See how it directly jumps to the top.

- Using smooth behavior: See how it goes smoothly to the top.
Similar Reads
How to create a Scroll To Bottom button in React JS ?
Learn to implement a "Scroll to Bottom" button in React using the useState() hook, a feature commonly seen in chat applications like WhatsApp and Telegram for effortless navigation to the end of a chat window. Prerequisites:NodeJS or NPMReact JSstyled-componentReact useState()Steps to Create the Rea
2 min read
How to Create a Scroll Back-to-Top Button in WordPress?
The "Scroll Back-to-Top Button" in the website is a feature that looks like a button in the website, typically in the lower corner of the screen, which allows users to easily scroll back to the top of the page with a single click. A "Back to Top" button is a very easy little feature you often see on
3 min read
How to Create a Scroll Back to Top button using CSS?
Scrolling to the top of a long webpage can be tedious, especially when you're at the bottom. A "Scroll to Top" button can improve user experience by allowing quick navigation back to the top with a single click, saving time and effort. This is particularly useful for mobile users who might otherwise
3 min read
Create Animated Scroll-to-Top Button in Tailwind CSS
An animated scroll-to-top button is a user interface element that appears on a webpage when the user scrolls down. It allows the user to quickly return to the top of the page with a single click or tap. This feature is often implemented using CSS animations or transitions, and it can enhance the use
2 min read
How to create Shopping Cart Button in ReactJS?
A shopping cart button is one of the most used component in e-commerce websites or applications which allows users to add items to their cart . In this tutorial, we will learn how to create a shopping cart button in ReactJS using Material-UI. PrerequisitesJavaScript and JSXReact JSSteps to create Re
3 min read
How to create Like Button Using React JS ?
This is a social media world and the like button is one of the main components of it. From social media accounts to shopping platforms like buttons are everywhere. In this tutorial, we'll explore how to create a dynamic button in a React application. The button will change its appearance based on di
4 min read
How to scroll to a particular element or skip to content in React JS ?
In this article, we will learn about how to scroll or skip a particular element in React JS, We can do this using React hook useRef and React styled-components. Prerequisite:ReactReact Styled Components.React useRef hooks.Approach to implement particular element scroll/skip: It is used to scroll to
4 min read
How to create Popup box in React JS ?
Popup boxes, also known as modal or dialog boxes, are common UI elements used to display additional content or interactions on top of the main page. Creating a popup box in React can be achieved using various approaches, including libraries or custom implementations. We will use the Reactjs-popup li
2 min read
How to scroll to top on every Route click in Angular5 ?
We can use Router and NavigationEnd from '@angular/router' and hence we can scroll to the top of the webpage for every route.Approach:First, we need to import the Router and NavigationEnd from '@angular/router' in both app.module.ts file and app.component.ts.Then we need to create an instance of tho
2 min read
Write code to render a button in React JS
React is a UI library. It renders components written in JSX.  You can build and render any components as per your usage. In this article, we will see how to render a button in React JS. Prerequisites:NodeJS or NPMReact JSJSX SyntaxApproach to render a button: Let us create a React project and a UI t
2 min read