generated from goitacademy/react-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring ScrollButton using hooks
- Loading branch information
1 parent
7e9e778
commit 1ff705f
Showing
2 changed files
with
38 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,43 @@ | ||
import React, { Component } from 'react'; | ||
import React, { useEffect, useState } from 'react'; | ||
import styles from './ScrollButton.module.css'; | ||
|
||
class ScrollButton extends Component { | ||
componentDidMount() { | ||
window.addEventListener('scroll', this.scrollFunction); | ||
} | ||
const ScrollButton = () => { | ||
const [displayButton, setDisplayButton] = useState(false); | ||
|
||
componentWillUnmount() { | ||
window.removeEventListener('scroll', this.scrollFunction); | ||
} | ||
useEffect(() => { | ||
const handleScroll = () => { | ||
if (window.scrollY > 20) { | ||
setDisplayButton(true); | ||
} else { | ||
setDisplayButton(false); | ||
} | ||
}; | ||
|
||
scrollFunction = () => { | ||
const mybutton = document.getElementById('btn-back-to-top'); | ||
if ( | ||
document.body.scrollTop > 20 || | ||
document.documentElement.scrollTop > 20 | ||
) { | ||
mybutton.style.display = 'block'; | ||
} else { | ||
mybutton.style.display = 'none'; | ||
} | ||
}; | ||
window.addEventListener('scroll', handleScroll); | ||
|
||
return () => { | ||
window.removeEventListener('scroll', handleScroll); | ||
}; | ||
}, []); | ||
|
||
backToTop = () => { | ||
const backToTop = () => { | ||
window.scrollTo({ | ||
top: 0, | ||
behavior: 'smooth', | ||
}); | ||
}; | ||
|
||
render() { | ||
return ( | ||
<button | ||
id="btn-back-to-top" | ||
className={styles.ScrollButton} | ||
onClick={this.backToTop} | ||
> | ||
Scroll to Top | ||
</button> | ||
); | ||
} | ||
} | ||
return ( | ||
<button | ||
id="btn-back-to-top" | ||
className={`${styles.ScrollButton} ${ | ||
displayButton ? styles.show : styles.hide | ||
}`} | ||
onClick={backToTop} | ||
> | ||
Scroll to Top | ||
</button> | ||
); | ||
}; | ||
|
||
export default ScrollButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,11 @@ | |
.ScrollButton:active { | ||
transform: scale(0.95); | ||
} | ||
|
||
.show { | ||
display: block; | ||
} | ||
|
||
.hide { | ||
display: none; | ||
} |