Snap Scroll in React in Less than a Minute

Motivation:

We all have seen website which uses snap scrolling from one section to another traditionally we can do Animation in CSS and write a ton styles to achieve that Let’s do that within a Minute with help of react-use-scroll-snap.


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Ranjan Singh

Motivation:

We all have seen website which uses snap scrolling from one section to another traditionally we can do Animation in CSS and write a ton styles to achieve that Let's do that within a Minute with help of react-use-scroll-snap.

Installation:

we just need to create a new react app and install react-use-scroll-snap.

npx create-react-app test-app
yarn add react-use-scroll-snap

Usage:

Import useScrollSnap hook and pass the ref of the component.
I'm using default react app duplication header and just changing the page name for simplicity you can use your components.

import { useRef } from "react";
import useScrollSnap from "react-use-scroll-snap";
import logo from "./logo.svg";
import "./App.css";

function App() {
  const scrollRef = useRef(null);
  useScrollSnap({ ref: scrollRef, duration: 50, delay: 20 });
  return (
    <div className="App" ref={scrollRef}>
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>Page 1</p>
      </header>
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>page 2</p>
      </header>
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>Page 3</p>
      </header>
    </div>
  );
}

export default App;

That's it we have snap scroll in our app. This post is focust on react you can do same thing for Vanilla JS with scroll-snap Cheers.


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Ranjan Singh


Print Share Comment Cite Upload Translate Updates
APA

Ranjan Singh | Sciencx (2022-10-01T17:56:39+00:00) Snap Scroll in React in Less than a Minute. Retrieved from https://www.scien.cx/2022/10/01/snap-scroll-in-react-in-less-than-a-minute/

MLA
" » Snap Scroll in React in Less than a Minute." Ranjan Singh | Sciencx - Saturday October 1, 2022, https://www.scien.cx/2022/10/01/snap-scroll-in-react-in-less-than-a-minute/
HARVARD
Ranjan Singh | Sciencx Saturday October 1, 2022 » Snap Scroll in React in Less than a Minute., viewed ,<https://www.scien.cx/2022/10/01/snap-scroll-in-react-in-less-than-a-minute/>
VANCOUVER
Ranjan Singh | Sciencx - » Snap Scroll in React in Less than a Minute. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/10/01/snap-scroll-in-react-in-less-than-a-minute/
CHICAGO
" » Snap Scroll in React in Less than a Minute." Ranjan Singh | Sciencx - Accessed . https://www.scien.cx/2022/10/01/snap-scroll-in-react-in-less-than-a-minute/
IEEE
" » Snap Scroll in React in Less than a Minute." Ranjan Singh | Sciencx [Online]. Available: https://www.scien.cx/2022/10/01/snap-scroll-in-react-in-less-than-a-minute/. [Accessed: ]
rf:citation
» Snap Scroll in React in Less than a Minute | Ranjan Singh | Sciencx | https://www.scien.cx/2022/10/01/snap-scroll-in-react-in-less-than-a-minute/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.