How to scroll to a particular element or skip to content in React JS ?
Last Updated :
12 Dec, 2023
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:
Approach to implement particular element scroll/skip:
It is used to scroll to the specified element in the browser. Here, we use top or left or right or bottom as the first parameter to scroll the page in the desired direction.
The second parameter is the behavior (of the scroll). It tells us how the window is going to reach the specified element and has a default value of the auto. However, it's not compulsory to provide this parameter as the browser uses its default value in case it's not provided.
Types of behavior:
- Auto behavior: It allows a straight jump "scroll effect" and takes us to the element. This is the default value of the behavior parameter as discussed above.
- Smooth behavior: It allows a smooth "scroll effect" through the page and takes us to the element.
Syntax:
//Auto behavior or Smooth behavior syntax
window.scrollTo({
top: 100px,
behavior: "auto" or "smooth"
});
Steps to Create React Application And Installing Module:
Step 1: Create a React application using the following command:
npx create-react-app react-scroll
Step 2: After creating your project folder i.e. example, move to it using the following command:
cd react-scroll
Step 3: Installing Required module dependencies:
npm install --save styled-components
Project Structure:

The updated dependencies in package.json file will look like:
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"styled-components": "^6.1.1",
"web-vitals": "^2.1.4",
}
Example: When we click on the button the function gotoServices is getting triggered through onClick event. This top position of the Services element is accessed by the offsetTop property by writing Services.current.offsetTop which returns the top position (in pixels) relative to the top of the offsetParent element.
JavaScript
import { Fragment } from "react";
import ScrollPage from "./components/ScrollPage";
function App() {
return (
<Fragment>
<ScrollPage />
</Fragment>
);
}
export default App;
JavaScript
//Styles.js
import styled from 'styled-components';
export const Heading = styled.h1`
margin: 0;
padding: 0;
text-align: center;
color: green;
`;
export const Button = styled.button`
padding: 20px;
font-size: 20px;
position: relative;
left: 42%;
margin: 20px;
`;
export const Para = styled.p`
padding-left: 40px;
padding-right: 40px;
`;
export const Margin = styled.div`
margin-top: 610px;
margin-bottom: 500px;
background-color: black;
color: white;
padding: 20px;
`;
JavaScript
//ScrollPage.js
import React, { useRef } from "react";
import { Heading, Button, Para, Margin } from "./Styles";
const ScrollPage = () => {
const ServicesRef = useRef(null);
const gotoServices = () =>
window.scrollTo({
top: ServicesRef.current.offsetTop,
behavior: "smooth",
// You can also assign value "auto"
// to the behavior parameter.
});
return (
<div>
<Heading>GeeksForGeeks</Heading>
<Button onClick={gotoServices}>Scroll to Services</Button>
<Margin ref={ServicesRef}>
<Heading>GeeksForGeeks Services</Heading>
<Para>
Lorem Ipsum è un testo segnaposto utilizzato
nel settore della tipografia e della stampa.
Lorem Ipsum è considerato il testo segnaposto
standard sin dal sedicesimo secolo, quando un anonimo
tipografo prese una cassetta di caratteri e li
assemblò per preparare un testo campione. È
sopravvissuto non solo a più di cinque secoli, ma
anche al passaggio alla videoimpaginazione, pervenendoci
sostanzialmente inalterato. Fu reso popolare, negli
anni ’60, con la diffusione dei fogli di caratteri
trasferibili “Letraset”, che contenevano passaggi del
Lorem Ipsum, e più recentemente da software
di impaginazione come Aldus PageMaker, che includeva
versioni del Lorem Ipsum.
</Para>
<Para>
Lorem Ipsum è un testo segnaposto utilizzato nel
settore della tipografia e della stampa. Lorem
Ipsum è considerato il testo segnaposto standard
sin dal sedicesimo secolo, quando un anonimo tipografo
prese una cassetta di caratteri e li assemblò per preparare
un testo campione. È sopravvissuto non solo a più di
cinque secoli, ma anche al passaggio alla videoimpaginazione,
pervenendoci sostanzialmente inalterato. Fu reso popolare,
negli anni ’60, con la diffusione dei fogli di caratteri
trasferibili “Letraset”, che contenevano passaggi del Lorem
Ipsum, e più recentemente da software di impaginazione come
Aldus PageMaker, che includeva versioni del Lorem Ipsum.
</Para>
</Margin>
</div>
);
};
export default ScrollPage;
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
Using default behavior (auto): See how it directly jumps to the element. 
Using smooth behavior: See how it goes smoothly through the page. 
Similar Reads
How to scroll to a particular element or skip the content in CSS ?
Sometimes we need the user to click on a button or link and take them to an element present on the same HTML page. Also, this needs to take the user to the element without any reload. Lots of times, users tend to skip to the main content, like on a gaming Website. They might want to skip the instruc
4 min read
How to Create a Scroll To Top Button in React JS ?
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 Re
3 min read
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 rotate a particular string onScroll in ReactJS ?
In, this article, we will see how we can rotate a text while Scrolling to give a nice user experience by using an npm package react-scroll-rotate.Prerequisites:NodeJS or NPMReact JSSyntax:<ScrollRotate> </ScrollRotate>We wrap the text within these tags which we want to show effects.Steps
2 min read
How to Scroll to an Element Inside a Div using JavaScript?
To scroll to an element within a div using JavaScript, set the parent div's scrollTop`to the target element's offsetTop. This method allows smooth navigation within a scrollable container. Below are the methods to scroll to an element inside a Div using JavaScript:Table of Content Using scrollIntoVi
3 min read
How To Scroll To An HTML Element In NextJS?
NextJS has various features that enable smooth scrolling to HTML elements. One method involves using useRef to create a reference to the target element and scrollIntoView to achieve smooth scrolling. Another method, using useEffect and window.scrollBy, sets up an event listener on a button for smoot
3 min read
How to add Event Listeners to wrapped Component in ReactJS ?
React provides a powerful way of DOM manipulation by introducing the concept of virtual DOM. It is mainly used to build single-page applications. There are times when you need to add event listeners. This can be achieved by using the concept of forwarding refs and leveraging React's lifecycle method
2 min read
How to change the position of the element dynamically in ReactJS ?
To change the position of an element dynamically based on certain conditions or user interactions, we can access and modify its styling and link to certain events. When these events trigger that in return will change the position of the elementPrerequisitesReact JSNPM and Node.jsReact JS Class Compo
2 min read
How to trigger animations in a ReactJS app based on the scroll position ?
To trigger animations in a ReactJS app based on the scroll position we can define multiple component animations for the scroll values provided by windows.scrollY. When we scroll to that defined position, some animations get triggered. This provides a very nice user experience.Prerequisite:Â React JSN
4 min read
How to change the navbar color when you scroll in ReactJS ?
On scroll navbar color change in React highlights a sticky responsive navigation bar to navigate through web application pages. It provides an efficient way to navigate multiple pages in a single-page application. The following approach covers how to change the navbar color when you scroll through t
4 min read