Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react how to scroll to element

import React, { useRef } from 'react'

const scrollToRef = (ref) => window.scrollTo(0, ref.current.offsetTop)   
// General scroll to element function

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> 
      </>
   )
}
Comment

scroll to section react

import React, { useRef } from 'react'

const scrollToRef = (ref) => window.scrollTo({ top: ref.current.offsetTop, behavior: "smooth" })   
// General scroll to element function

const ScrollToSection = () => {

   const sectionRef = useRef(null)
   const executeSectionScroll = () => scrollToRef(myRef)

   return (
      <> 
         <div ref={sectionRef}>I wanna be seen</div> 
         <button onClick={executeSectionScroll}> Click to scroll </button> 
      </>
   )
}
Comment

react how to scroll to element

class ReadyToScroll extends Component {

    constructor(props) {
        super(props)
        this.myRef = React.createRef()  
    }

    render() {
        return <div ref={this.myRef}></div> 
    }  

    scrollToMyRef = () => window.scrollTo(0, this.myRef.current.offsetTop)   
    // run this method to execute scrolling. 

}
Comment

react link scroll to section

import { HashLink } from 'react-router-hash-link';

<HashLink smooth to="/path#hash">
  Link to Hash Fragment
</HashLink>;
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery select div in div 
Javascript :: jquery select clear options 
Javascript :: time calculator js 
Javascript :: add access-control-allow-origin in node js 
Javascript :: javascript json decode 
Javascript :: javascript change dataset value 
Javascript :: angular checkbox disabled 
Javascript :: async await anonymous function 
Javascript :: why do you have to set key prop in react 
Javascript :: js array copy not reference 
Javascript :: redux persist a non-serializable value was detected in an action in the path register 
Javascript :: is_int js 
Javascript :: get url params with js 
Javascript :: read json from file js 
Javascript :: select document jquery 
Javascript :: toast in react native 
Javascript :: jquery has attribute 
Javascript :: javascript get last url segment 
Javascript :: delay statement in js 
Javascript :: lodash remove undefined values from object 
Javascript :: contenteditable javascript 
Javascript :: get datetime yesterday angular 
Javascript :: readfilesync return buffer 
Javascript :: see all functions in a website with console 
Javascript :: js sort numbers descending order 
Javascript :: Send Email using AWS SES and Lambda 
Javascript :: append to array js 
Javascript :: react routes 
Javascript :: get value from json.stringify 
Javascript :: remove event listener jquery 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =