DEV Community

Cover image for How to create a Scroll to Top Button with React

How to create a Scroll to Top Button with React

Silvia EspaƱa Gil on August 24, 2021

Hola Mundo! So this is my first coding post and am really excited about it šŸ‘©ā€šŸŽ¤āœØ Thing is that, when I was creating my Portfolio I de...
Collapse
 
miketalbot profile image
Mike Talbot ā­

Really nice post :)

One thing - your useEffect(()=>window.addListener(... is being called on every re-render so you will end up with lots of them added. You should return a function to remove the listener so it tidies up and possibly rewrite things a bit so you can actually only do it on mount/unmount with deps of []. Care needs to be taken with that though because the you need to be sure that the handler can work in cases when it wasn't added on the same render (it's a closure on the time the deps change).

Collapse
 
silviaespanagil profile image
Silvia EspaƱa Gil

Thank you very much Mike, I'll put at eye on it and see if I find a solution so it works in every scenario!

Collapse
 
hassankhademi profile image
hassanKhademi

useEffect(() => {
window.addEventListener("scroll", handleVisibleButton);
},[]);
For fix issue this way is great
execute when component mounting
One listener

Collapse
 
hashcode01 profile image
Hash

what about removing the listener when unmouning,


useEffect(() => {
const e = window.addEventListener("scroll", handleVisibleButton);

return ()=> window.removeEventListener('scroll',handleVisibleButton);
},[]);

Enter fullscreen mode Exit fullscreen mode
Collapse
 
ottovector profile image
Otto Vector • Edited

This (I think) more shorter and in one component
No styles info, because it is very hard to qick-understand.
You can Use youre beatiful style.

///////////////////////////////////////////////////
import React, { useEffect, useState } from 'react'
import styles from './go-top-button.module.scss'

const GoTopButton = () => {

const [ showGoTop, setShowGoTop ] = useState( false )

const handleVisibleButton = () => {
    setShowGoTop( window.pageYOffset > 50 )
}

const handleScrollUp = () => {
    window.scrollTo( { left: 0, top: 0, behavior: 'smooth' } )
}

useEffect( () => {
    window.addEventListener( 'scroll', handleVisibleButton )
}, [] )

return (
    <div className={ showGoTop ? '' : styles.goTopHidden } onClick={ handleScrollUp }>
        <button type={'button'} className={ styles.goTop }>
            //use Material Icon
            <span className={ styles.goTopIcon }>expand_less</span>
        </button>
    </div>
)
Enter fullscreen mode Exit fullscreen mode

}

export default GoTopButton

Collapse
 
shehabadel profile image
Shehab Adel

Thank you so much both! @silviaespanagil @ottovector

Collapse
 
chetan_atrawalkar profile image
Chetan Atrawalkar

Thank you so much for this post because I'm already working on this for my personal portfolio šŸ¤—šŸ’œ @silviaespanagil

Collapse
 
silviaespanagil profile image
Silvia EspaƱa Gil

Thank you Chetan, I hope it helps you and that you show us your finished portfolio!

Collapse
 
chetan_atrawalkar profile image
Chetan Atrawalkar

Most welcome and yes definitely.

Collapse
 
chetan_atrawalkar profile image
Chetan Atrawalkar

Where I'm connect with you for conversation? @silviaespanagil

Thread Thread
 
silviaespanagil profile image
Silvia EspaƱa Gil

Hi Chetan, I just send a connect invitation vĆ­a LinkedIn

Thread Thread
 
chetan_atrawalkar profile image
Chetan Atrawalkar

ok thanks i will check

Collapse
 
bvince77 profile image
bvince77

Nice little feature

Collapse
 
sajibsrs profile image
Sajidur Rahman

I joined here, to encourage you. As you are very excited about your first post. That's a nice post. Keep it up ā˜ŗļø

Collapse
 
silviaespanagil profile image
Silvia EspaƱa Gil

Thank you very much Sajidur!

Collapse
 
erlangga092 profile image
Erlangga

Nice
Thanks