![Create React App Officially Deprecated Amid React 19 Compatibility Issues](https://cdn.sanity.io/images/cgdhsj6q/production/04fa08cf844d798abc0e1a6391c129363cc7e2ab-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Create React App Officially Deprecated Amid React 19 Compatibility Issues
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
react-scroll
Advanced tools
The react-scroll package provides smooth scrolling functionality for React applications. It allows developers to create scrollable components and implement smooth scrolling animations to specific elements within a page.
Smooth Scrolling to Elements
This feature allows you to create links that smoothly scroll to specific elements on the page. The 'Link' component is used to create the scrollable link, and the 'Element' component is used to define the target section.
import { Link, Element } from 'react-scroll';
function App() {
return (
<div>
<Link to="section1" smooth={true} duration={500}>Go to Section 1</Link>
<Element name="section1" className="element">
Section 1
</Element>
</div>
);
}
Scroll Events
This feature allows you to register and handle scroll events. You can log or perform actions when scrolling begins or ends. The 'animateScroll' function can be used to programmatically scroll to specific positions.
import { Events, animateScroll as scroll, scroller } from 'react-scroll';
function App() {
useEffect(() => {
Events.scrollEvent.register('begin', function() {
console.log('begin', arguments);
});
Events.scrollEvent.register('end', function() {
console.log('end', arguments);
});
return () => {
Events.scrollEvent.remove('begin');
Events.scrollEvent.remove('end');
};
}, []);
return (
<div>
<button onClick={() => scroll.scrollToTop()}>Scroll to Top</button>
</div>
);
}
Scroll to Top
This feature provides a simple way to scroll to the top of the page programmatically. The 'scrollToTop' function from 'animateScroll' is used to achieve this.
import { animateScroll as scroll } from 'react-scroll';
function App() {
return (
<div>
<button onClick={() => scroll.scrollToTop()}>Scroll to Top</button>
</div>
);
}
The react-router-hash-link package provides smooth scrolling to hash links within a React Router application. It integrates with React Router to enable smooth scrolling to elements identified by hash links. Compared to react-scroll, it is more focused on hash-based navigation within React Router.
The react-scrollable-anchor package allows for smooth scrolling to anchor links within a React application. It provides a simple API to create scrollable anchor links. Compared to react-scroll, it is more focused on anchor-based navigation and does not provide as many customization options.
The react-scrollspy package provides scrollspy functionality for React applications. It allows you to highlight navigation links based on the current scroll position. Compared to react-scroll, it focuses on tracking scroll positions and updating navigation links accordingly.
React component for animating vertical scrolling
React-Scroll is a lightweight library for enhancing scrolling functionality in React applications. Whether you're building a single-page application, a portfolio site, or a complex web application, React Scroll provides you with an easy-to-use solution for implementing smooth scrolling behavior. This works with the latest versions of Google Chrome, Microsodt Edge, and Firefox.
React-Scroll Bot will help you understand this repository better. You can ask for code examples, installation guide, debugging help and much more.
$ npm install react-scroll
or
$ yarn add react-scroll
$ npm install
$ npm test
$ npm start
or
$ yarn
$ yarn test
$ yarn start
Checkout examples
Live example
Code example
In this example, the react-scroll library was utilized to enable smooth scrolling navigation within a single-page React application. The library provides components such as Link and Element that facilitate seamless navigation between different sections of the page. Once you start your react app, you can add this code at the bottom to experience the scroll feature!
Code:
import React from 'react';
import { Link, Element } from 'react-scroll';
function App() {
return (
<div>
<nav>
<ul>
<li>
<Link to="section1" smooth={true} duration={500}>Section 1</Link>
</li>
<li>
<Link to="section2" smooth={true} duration={500}>Section 2</Link>
</li>
{/* Add more navigation links as needed */}
</ul>
</nav>
<Element name="section1">
<section style={{ height: '100vh', backgroundColor: 'lightblue' }}>
<h1>Section 1</h1>
<p>This is the content of section 1</p>
</section>
</Element>
<Element name="section2">
<section style={{ height: '100vh', backgroundColor: 'lightgreen' }}>
<h1>Section 2</h1>
<p>This is the content of section 2</p>
</section>
</Element>
{/* Add more sections with Element components as needed */}
</div>
);
}
export default App;
import React, { useEffect } from 'react';
import { Link, Button, Element, Events, animateScroll as scroll, scrollSpy } from 'react-scroll';
const Section = () => {
// useEffect is used to perform side effects in functional components.
// Here, it's used to register scroll events and update scrollSpy when the component mounts.
useEffect(() => {
// Registering the 'begin' event and logging it to the console when triggered.
Events.scrollEvent.register('begin', (to, element) => {
console.log('begin', to, element);
});
// Registering the 'end' event and logging it to the console when triggered.
Events.scrollEvent.register('end', (to, element) => {
console.log('end', to, element);
});
// Updating scrollSpy when the component mounts.
scrollSpy.update();
// Returning a cleanup function to remove the registered events when the component unmounts.
return () => {
Events.scrollEvent.remove('begin');
Events.scrollEvent.remove('end');
};
}, []);
// Defining functions to perform different types of scrolling.
const scrollToTop = () => {
scroll.scrollToTop();
};
const scrollToBottom = () => {
scroll.scrollToBottom();
};
const scrollTo = () => {
scroll.scrollTo(100); // Scrolling to 100px from the top of the page.
};
const scrollMore = () => {
scroll.scrollMore(100); // Scrolling an additional 100px from the current scroll position.
};
// Function to handle the activation of a link.
const handleSetActive = (to) => {
console.log(to);
};
// Rendering the component's JSX.
return (
<div>
{/* Link component to scroll to "test1" element with specified properties */}
<Link
activeClass="active"
to="test1"
spy={true}
smooth={true}
offset={50}
duration={500}
onSetActive={handleSetActive}
>
Test 1
</Link>
{/* Other Link and Button components for navigation, each with their unique properties and targets */}
{/* ... */}
{/* Element components that act as scroll targets */}
<Element name="test1" className="element">
test 1
</Element>
<Element name="test2" className="element">
test 2
</Element>
<div id="anchor" className="element">
test 6 (anchor)
</div>
{/* Links to elements inside a specific container */}
<Link to="firstInsideContainer" containerId="containerElement">
Go to first element inside container
</Link>
<Link to="secondInsideContainer" containerId="containerElement">
Go to second element inside container
</Link>
{/* Container with elements inside */}
<div className="element" id="containerElement">
<Element name="firstInsideContainer">
first element inside container
</Element>
<Element name="secondInsideContainer">
second element inside container
</Element>
</div>
{/* Anchors to trigger scroll actions */}
<a onClick={scrollToTop}>To the top!</a>
<br/>
<a onClick={scrollToBottom}>To the bottom!</a>
<br/>
<a onClick={scrollTo}>Scroll to 100px from the top</a>
<br/>
<a onClick={scrollMore}>Scroll 100px more from the current position!</a>
</div>
);
};
export default Section;
activeClass | class applied when element is reached |
activeStyle | style applied when element is reached |
to | Target to scroll to |
containerId | Container to listen for scroll events and to perform scrolling in |
spy | Make Link selected when scroll is at its targets position |
hashSpy | Update hash based on spy, containerId has to be set to scroll a specific element |
smooth | Animate the scrolling |
offset | Scroll additional px ( like padding ) |
duration | time of the scroll animation - can be a number or a function (`function (scrollDistanceInPx) { return duration; }`), that allows more granular control at run-time |
delay | Wait x milliseconds before scroll |
isDynamic | In case the distance has to be recalculated - if you have content that expands etc. |
onSetActive | Invoke whenever link is being set to active |
onSetInactive | Invoke whenever link is lose the active status |
ignoreCancelEvents | Ignores events which cancel animated scrolling |
horizontal | Whether to scroll vertically (`false`) or horizontally (`true`) - default: `false` |
spyThrottle | Time of the spy throttle - can be a number |
<Link activeClass="active"
to="target"
spy={true}
smooth={true}
hashSpy={true}
offset={50}
duration={500}
delay={1000}
isDynamic={true}
onSetActive={this.handleSetActive}
onSetInactive={this.handleSetInactive}
ignoreCancelEvents={false}
spyThrottle={500}
>
Your name
</Link>
Scroll To Top
import { animateScroll } from 'react-scroll';
const options = {
// your options here, for example:
duration: 500,
smooth: true,
};
animateScroll.scrollToTop(options);
Scroll To Bottom
import { animateScroll } from 'react-scroll';
const options = {
// Your options here, for example:
duration: 500,
smooth: true,
};
animateScroll.scrollToBottom(options);
Scroll To (position)
import { animateScroll } from 'react-scroll';
const options = {
// Your options here, for example:
duration: 500,
smooth: true,
};
// Scroll to 100 pixels from the top of the page
animateScroll.scrollTo(100, options);
Scroll To (Element)
animateScroll.scrollTo(positionInPixels, props = {});
import { Element, scroller } from 'react-scroll';
<Element name="myScrollToElement"></Element>
// Somewhere else, even another file
scroller.scrollTo('myScrollToElement', {
duration: 1500,
delay: 100,
smooth: true,
containerId: 'ContainerElementID',
offset: 50, // Scrolls to element + 50 pixels down the page
// ... other options
});
Scroll More (px)
import { animateScroll } from 'react-scroll';
const options = {
// Your options here, for example:
duration: 500,
smooth: true,
};
// Scroll an additional 10 pixels down from the current scroll position
animateScroll.scrollMore(10, options);
begin - start of the scrolling
import { Events } from 'react-scroll';
Events.scrollEvent.register('begin', function(to, element) {
console.log('begin', to, element);
});
end - end of the scrolling/animation
import { Events } from 'react-scroll';
Events.scrollEvent.register('end', function(to, element) {
console.log('end', to, element);
});
Remove events
import { Events } from 'react-scroll';
Events.scrollEvent.remove('begin');
Events.scrollEvent.remove('end');
Simply just pass your component to one of the high order components (Element/Scroll)
import React from 'react';
import { ScrollElement, ScrollLink } from 'react-scroll';
const Element = (props) => {
return (
<div {...props} ref={(el) => { props.parentBindings.domNode = el; }}>
{props.children}
</div>
);
};
export const ScrollableElement = ScrollElement(Element);
const Link = (props) => {
return (
<a {...props}>
{props.children}
</a>
);
};
export const ScrollableLink = ScrollLink(Link);
Add a custom easing animation to the smooth option. This prop will accept a Boolean if you want the default, or any of the animations listed below
import { scroller } from 'react-scroll';
scroller.scrollTo('myScrollToElement', {
duration: 1500,
delay: 100,
smooth: 'easeInOutQuint',
containerId: 'ContainerElementID',
// ... other options
});
List of currently available animations:
linear
- no easing, no acceleration.
easeInQuad
- accelerating from zero velocity.
easeOutQuad
- decelerating to zero velocity.
easeInOutQuad
- acceleration until halfway, then deceleration.
easeInCubic
- accelerating from zero velocity.
easeOutCubic
- decelerating to zero velocity.
easeInOutCubic
- acceleration until halfway, then deceleration.
easeInQuart
- accelerating from zero velocity.
easeOutQuart
- decelerating to zero velocity.
easeInOutQuart
- acceleration until halfway, then deceleration.
easeInQuint
- accelerating from zero velocity.
easeOutQuint
- decelerating to zero velocity.
easeInOutQuint
- acceleration until halfway, then deceleration.
A good visual reference can be found at easings.net
Q: How do I customize the scroll behavior?
A: You can customize the scroll duration, easing function, and other parameters using the duration, smooth, and offset props.
Q: Why is my smooth scrolling not working? This can be applied to any prop!
A: Ensure that the smooth prop is set to true and that your browser supports smooth scrolling.
To contribute to React-Scroll, please follow these guidelines:
Fork the repository and create a new branch for your changes.
Make your changes and submit a pull request with a clear description of your work.
Include tests and ensure all existing tests pass before submitting your changes.
FAQs
A scroll component for React.js
The npm package react-scroll receives a total of 464,695 weekly downloads. As such, react-scroll popularity was classified as popular.
We found that react-scroll demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.