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

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

PREVIOUS NEXT
Code Example
Javascript :: react capitalize first letter 
Javascript :: add active class and remove active class by click 
Javascript :: find all subsets of an array javascript 
Javascript :: slide out navigation 
Javascript :: how to cache data in javascript 
Javascript :: js clear all select options 
Javascript :: addclass removeclass jquery 
Javascript :: change image src onclick javascript 
Javascript :: javascript consecutive numbers in array 
Javascript :: jsonarray add jsonobject 
Javascript :: javascript radio button value if checked 
Javascript :: javascript string to lowercase 
Javascript :: react aos animation 
Javascript :: jquery attr 
Javascript :: express js npm 
Javascript :: axios react 
Javascript :: first repeated character in a string javascript 
Javascript :: javascript new date dd/mm/yyyy 
Javascript :: worker timeout 
Javascript :: js copy to clipboard 
Javascript :: virtual dom explained 
Javascript :: Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html" 
Javascript :: how to remove an object from array in react native 
Javascript :: javascript override shortcut 
Javascript :: json update pytohn 
Javascript :: "SyntaxError: Unexpected token o in JSON at position 1 
Javascript :: execute bash program using js 
Javascript :: javascript redirect to url with parameters 
Javascript :: vuex state from another module 
Javascript :: node print variable 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =