This file contains hidden or 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
| useEffect(() => reversibleEvent(window, 'click', () => alert('clicked!')), []); |
This file contains hidden or 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
| export const reversibleEvent = (target, listener, callback) => { | |
| target.addEventListener(listener, callback); | |
| return () => target.removeEventListener(listener, callback); | |
| }; |
This file contains hidden or 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
| useEffect(() => { | |
| const cleanup = reversibleTimeout(() => alert('one second has passed!'), 1000); | |
| return cleanup; | |
| }, []); | |
| // which we can simplify further to ⬇ | |
| useEffect(() => reversibleTimeout(() => alert('one second has passed!'), 1000), []); |
This file contains hidden or 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
| export const reversibleTimeout = (callback, ms) => { | |
| const timeoutId = setTimeout(callback, ms); | |
| return () => clearTimeout(timeoutId); | |
| }; |
This file contains hidden or 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
| useEffect(() => { | |
| const myCallback = () => { | |
| alert('clicked!'); | |
| }; | |
| window.addEventListener('click', myCallback); | |
| return () => { | |
| window.removeEventListener('click', myCallback); | |
| }; | |
| }, []); |
This file contains hidden or 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
| useEffect(() => { | |
| const timeoutId = setTimeout(() => alert('one second has passed!'), 1000); | |
| return () => { | |
| clearTimeout(timeoutId); | |
| }; | |
| }, []); |
This file contains hidden or 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
| useEffect(() => { | |
| setTimeout(() => alert('one second has passed!'), 1000); | |
| }, []); |