React Js Scroll to Element

Do you want to learn how to scroll to any element on your page using React JS? Whether you want to scroll to an element by id, scroll to a specific element, scroll to a div, or scroll to an element in a list, this article will show you how to do it easily and smoothly. You will learn how to use the scrollIntoView method, which is a native JavaScript function that scrolls the element into the visible area of the browser window. By the end of this article, you will be able to create a single-page app that scrolls to any element on your page with a click of a button




Thanks for your feedback!
Your contributions will help us to improve service.
How to Scroll to Specific Element in React?
React Js Scroll to Element by id:To scroll to an element by its ID in React.js, you can follow these steps. First, use the useRef hook to create a reference to the element with the specified ID. Next, add an event listener, such as a button click, and define a function to handle the scrolling. Inside the function, use the scrollIntoView method on the element's current property to scroll to it. Finally, call the function when the event occurs. This approach allows you to scroll to a specific element on the page based on its ID using React.js.
React Js Scroll to Element by id Example
xxxxxxxxxx
<script type="text/babel">
const { useState } = React
function App() {
const [elements, setElements] = useState([
{ id: 'element1', name: 'Element 1', description: 'This is the first element' },
{ id: 'element2', name: 'Element 2', description: 'This is the second element' },
{ id: 'element3', name: 'Element 3', description: 'This is the third element' }
]);
const scrollToElement = (id) => {
const container = document.getElementById(id);
container.scrollIntoView({ behavior: 'smooth' });
};
return (
<div className='container'>
<h3>React Scroll to element by id</h3>
{elements.map((element, index) => (
<button key={index} onClick={() => scrollToElement(element.id)}>
{element.name}
</button>
))}
{elements.map((element, index) => (
<div key={index} id={element.id} className="element">
<h2>{element.name}</h2>
<p>{element.description}</p>
</div>
))}
</div>
);
}
ReactDOM.render(<App />, document.getElementById("app"));
</script>
Output of React Scroll to Element
How to React Scroll into View?
Learn how to use the scrollIntoView function in React to smoothly scroll to any element on your web page. This Example will show you how to set up a ref on the target element, attach a click event listener on the trigger element, and call scrollIntoView in the listener
React Scroll to Specific Element Method 2
xxxxxxxxxx
<script type="text/babel">
function App() {
const scrollToElement = (elementId) => {
const element = document.getElementById(elementId);
if (element) {
element.scrollIntoView({ behavior: 'smooth' });
}
};
return (
<div className='container'>
<h3>React Js Scroll to Element</h3>
<button onClick={() => scrollToElement('div2')}>
Scroll to div2
</button>
<button onClick={() => scrollToElement('div3')}>
Scroll to div3
</button>
<div id="elementToScrollTo" style={{height:'300px', backgroundColor: 'blue' }}>
<h3>Element 1</h3>
{/* Content for the elementToScrollTo */}
</div>
<div id="div2" style={{ height: '300px',backgroundColor:'red' }}>
<h3>Element 2</h3>
{/* Content for the first additional div */}
</div>
<div id="div3" style={{ height: '300px', backgroundColor: 'yellow' }}>
<h3>Element 3</h3>
{/* Content for the second additional div */}
</div>
</div>
);
}
ReactDOM.render(<App />, document.getElementById("app"));
</script>
Output of React Js Scroll to Element
How do you scroll to the last item in a list using a ref in Reactjs?
This React.js code defines a component that displays a list of items and provides two buttons: "Scroll to Last Item" and "Show More Items." It uses hooks like useState and useRef. The state variable itemsToShow controls how many items are displayed, and it initially shows 5 items. Clicking "Show More Items" increments the count.
The scrollToLastItem function uses useRef to reference the last item in the list and scrolls to it smoothly when the "Scroll to Last Item" button is clicked. This provides a user-friendly scroll behavior.
Overall, the code dynamically manages item visibility and provides a convenient way to scroll to the last item in the list.
React Js Scroll to Last Item
xxxxxxxxxx
<script type="text/babel">
const { useState, useRef } = React
function App() {
const [itemsToShow, setItemsToShow] = useState(5);
const [items, setItems] = useState(['Laptop', 'Mobile', 'Backpack', 'Sunglasses', 'Water bottle', 'Headphones', 'Wallet', 'Umbrella', 'Notebook', 'Pen', 'Charger', 'Watch', 'Socks', 'Shoes', 'Jacket', 'Hat', 'Gloves', 'Towel', 'Sunscreen', 'Snacks', 'Map', 'First aid kit', 'Camera', 'Toothbrush', 'Toothpaste', 'Shampoo', 'Conditioner', 'Soap', 'Tissues', 'Hand sanitizer', 'Passport', 'Travel pillow', 'Blanket', 'Earplugs', 'Phone charger', 'Power bank', 'Sunglasses case', 'ID card', 'Cash', 'Credit cards', 'Earbuds', 'Lip balm', 'Hairbrush', 'Hair ties', 'Deodorant', 'Medications', 'Insect repellent'])
const newItemRef = useRef(null);
const scrollToLastItem = () => {
if (newItemRef.current) {
newItemRef.current.scrollIntoView({ behavior: 'smooth' });
}
};
const incrementItemsToShow = () => {
setItemsToShow(itemsToShow + 1);
};
return (
<div className='container'>
<div className='fixed-buttons'>
<button onClick={scrollToLastItem}>Scroll to Last Item</button>
<button onClick={incrementItemsToShow}>Show More Items</button>
</div>
<ul>
{items.slice(0, itemsToShow).map((item, index) => (
<li key={index} ref={index === itemsToShow - 1 ? newItemRef : null}>
{item}
</li>
))}
</ul>
</div>
);
}
ReactDOM.render(<App />, document.getElementById("app"));
</script>