How to scroll to an element? How to scroll to an element? reactjs reactjs

How to scroll to an element?


React 16.8 +, Functional component

const ScrollDemo = () => {   const myRef = useRef(null)   const executeScroll = () => myRef.current.scrollIntoView()       // run this function from an event handler or an effect to execute scroll    return (      <>          <div ref={myRef}>Element to scroll to</div>          <button onClick={executeScroll}> Click to scroll </button>       </>   )}

Click here for a full demo on StackBlits

React 16.3 +, Class component

class ReadyToScroll extends Component {    constructor(props) {        super(props)        this.myRef = React.createRef()      }    render() {        return <div ref={this.myRef}>Element to scroll to</div>     }      executeScroll = () => this.myRef.current.scrollIntoView()    // run this method to execute scrolling. }

Class component - Ref callback

class ReadyToScroll extends Component {      render() {        return <div ref={ (ref) => this.myRef=ref }>Element to scroll to</div>    }     executeScroll = () => this.myRef.scrollIntoView()    // run this method to execute scrolling. }

Don't use String refs.

String refs harm performance, aren't composable, and are on their way out (Aug 2018).

string refs have some issues, are considered legacy, and are likely tobe removed in one of the future releases. [Official React documentation]

resource1resource2

Optional: Smoothe scroll animation

/* css */html {    scroll-behavior: smooth;}

Passing ref to a child

We want the ref to be attached to a dom element, not to a react component. So when passing it to a child component we can't name the prop ref.

const MyComponent = () => {    const myRef = useRef(null)    return <ChildComp refProp={myRef}></ChildComp>} 

Then attach the ref prop to a dom element.

const ChildComp = (props) => {    return <div ref={props.refProp} />}


this worked for me

this.anyRef.current.scrollIntoView({ behavior: 'smooth', block: 'start' })

EDIT: I wanted to expand on this based on the comments.

const scrollTo = (ref) => {  if (ref && ref.current /* + other conditions */) {    ref.current.scrollIntoView({ behavior: 'smooth', block: 'start' })  }}<div ref={scrollTo}>Item</div>


Just find the top position of the element you've already determined https://www.w3schools.com/Jsref/prop_element_offsettop.asp then scroll to this position via scrollTo method https://www.w3schools.com/Jsref/met_win_scrollto.asp

Something like this should work:

handleScrollToElement(event) {  const tesNode = ReactDOM.findDOMNode(this.refs.test)  if (some_logic){    window.scrollTo(0, tesNode.offsetTop);  }}render() {  return (    <div>      <div ref="test"></div>    </div>)}

UPDATE:

since React v16.3 the React.createRef() is preferred

constructor(props) {  super(props);  this.myRef = React.createRef();}handleScrollToElement(event) {  if (<some_logic>){    window.scrollTo(0, this.myRef.current.offsetTop);  }}render() {  return (    <div>      <div ref={this.myRef}></div>    </div>)}