0

In react js I want to add one class attribute called "sticky" based on scroll position. I mean if the scroll position reached [div class="btnGrp"] then I need to add the class attribute "sticky" otherwise I need to remove that class attribute. How to solve this problem in react js. Here is the sample code i have tried.


const HideButton =()=>{

    useEffect()=>{
    navbar=document.getElementById('navbar');
    sticky=navbar.offsetTop;
    window.onscroll = function(){stickybar()};
}

const stickybar =()=>{
    pageYOffset =window.pageYOffset;
        if(window.pageYOffset <= sticky){
        navbar.classList.add("sticky");
    }else{
        navbar.classList.remove("sticky");
    }
}

    return (
        <>
            <p>What is Lorem Ipsum?
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
            <p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
            .
            .
            .
            .
            .
            .


            <p>Why do we use it?
            It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.</p>
            <p>Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
            <div class="btnGrp">
                <button>Reset</button>
                <button>Save</button>
            </div>
        </>
    )
}

If anything is required please let me know

  1. Can any one have any idea.
Rajendra Bar
  • 49
  • 1
  • 8
  • 1
    Does this answer your question? [Get scroll position with Reactjs](https://stackoverflow.com/questions/53158796/get-scroll-position-with-reactjs) – anuragb26 Apr 20 '21 at 04:23

1 Answers1

0

Consider the render hierarchy

  1. Repaint: Changing the color, opacity, visibility, etc.
  2. Reflow: Changing the width, dom structure, font size etc.
  3. Rerender: Changing the state have have that propagate through the react component tree.

Each one is more computationally intensive than the last so things that need to happen really smoothly, you want to stick to a lower level when possible. My point of saying all that is this: You don't want to use React to track this, you want to use vanilla JS to trigger the change and then reflect that in a state separately / afterwards.

So do something like this

const stickybar = () => {
  const navBar = useRef()
  
  useEffect(() => {
     navBar.current?.addEventListener("scroll", (e) => {
     //if (e.target.scrollTop is greater than the height of the screen
     // then add the stick class to the navBar
     //optionally set the state accordingly, if other aspects of the component need to know about this change
  }, [])

  return (<ref = {navBar}>
            <p>What is Lorem Ipsum?
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
            <p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
            .
            .
            .
            .
            .
            .


            <p>Why do we use it?
            It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.</p>
            <p>Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
            <div class="btnGrp">
                <button>Reset</button>
                <button>Save</button>
            </div>
        </>)

);

 
Cody E
  • 179
  • 11