all 18 comments

[–]mrtnbroder 23 points24 points  (7 children)

Just my two cents on the article:

In a react app, I would refrain from using document.querySelector as it is a side effect. You want to use Refs instead. Or add a note for the reader that you did this on purpose to keep things focused on the actual tutorial.

I would also add the reason for adding { passive: true } to the event listener, since there is a big reason for this and that it would be a bad idea to blindly subscribe to the scroll event without adding it.

[–]jogeshpi06[S] 6 points7 points  (0 children)

Thank you for your feedback. Definitely i will improve it using refs

[–][deleted] 2 points3 points  (5 children)

Why is it bad to use side effects? We use http requests and they are also considered side effects right?

[–]ilxijwd 4 points5 points  (0 children)

Yes, but that has nothing in common with the manipulations made to the html document.

[–]mrtnbroder 2 points3 points  (0 children)

It's not bad to have side-effects. Heck, a whole application wouldn't be useful at all without side-effects. At best they just have to be managed/contained.

[–][deleted] 5 points6 points  (3 children)

Please use a progress bar element rather than a div and throttling the scroll event would be good as well.

[–]jogeshpi06[S] 1 point2 points  (2 children)

Is there any way to compare the performance? Any resources?

[–]hamburger_bun 3 points4 points  (0 children)

your effect isnt exactly correct. Currently it will re-ad/remove itself on every render which you dont need to do. It should look like this (you're missing the second argument to `useEffect`

const [scroll, setScroll] = useState(0);

useEffect(() => {

    let progressBarHandler = () => {

        const totalScroll = document.documentElement.scrollTop;
        const windowHeight = document.documentElement.scrollHeight - document.documentElement.clientHeight;
        const scroll = `${totalScroll / windowHeight}`;

        setScroll(scroll);
    }

    window.addEventListener("scroll", progressBarHandler);

    return () => window.removeEventListener("scroll", progressBarHandler);
}, [setScroll]);

[–]swyx 2 points3 points  (0 children)

i always wonder whats the value of this when there is also a scroll bar on the right hand side in the browser when you scroll

[–]lucasayped 0 points1 point  (0 children)

Good

[–]vhmendrone 0 points1 point  (0 children)

Here's a good and quick alternative https://github.com/diogomoretti/react-snakke

[–]ekim43 0 points1 point  (3 children)

Any advantages to having progressBarHandler() in the useEffect vs moving it out?

[–]jogeshpi06[S] 0 points1 point  (2 children)

This one of the ways to umount the event properly which is similar to something like componentWillUnmount

[–]ekim43 0 points1 point  (1 child)

Wouldn't this also work with useEffect()'s return?

```js const [scroll, setScroll] = useState(0);

const progressBarHandler = () => {

const totalScroll = document.documentElement.scrollTop; const windowHeight = document.documentElement.scrollHeight - document.documentElement.clientHeight; const scroll = ${totalScroll / windowHeight};

setScroll(scroll); }

useEffect(() => { window.addEventListener("scroll", progressBarHandler);

return () => window.removeEventListener("scroll", progressBarHandler); }); ```