Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Scroll to top in react

I am adding the Scroll to top div in Footer section. After clicking on the scroll top icon it will go the top of page

After Clicking on the Scroll to top it will go the top of page

Here is my code

import React from 'react';

class ScrollTop extends React.Component {
    render() {
        return (
            <div className="scrolltop">
                <i className="fa fa-angle-up" />
            </div>
        );
    }
}

export default ScrollTop;
like image 294
Leela Hari Prasad. R Avatar asked Dec 13 '22 10:12

Leela Hari Prasad. R


1 Answers

Just use vanilla javascript

import React from 'react';

class ScrollTop extends React.Component {
    render() {
        return (
            <div className="scrolltop" onClick={() => window.scrollTo({top: 0, behavior: 'smooth'})}>
                <i className="fa fa-angle-up" />
            </div>
        );
    }
}

export default ScrollTop;
like image 124
Dupocas Avatar answered Dec 20 '22 21:12

Dupocas