react-scroll scroll to element function code example
Example 1: react how to scroll to element
import React, { useRef } from 'react'
const scrollToRef = (ref) => window.scrollTo(0, ref.current.offsetTop)
const ScrollDemo = () => {
const myRef = useRef(null)
const executeScroll = () => scrollToRef(myRef)
return (
<>
<div ref={myRef}>I wanna be seen</div>
<button onClick={executeScroll}> Click to scroll </button>
</>
)
}
Example 2: react-scroll
$ npm install react-scroll
# or yarn
$ yarn add react-scroll