-1

I am trying to get the scroll position of a div in React. I tried getting the window the scroll values the values are always 0.

  handleScrollPosition(e){

   sessionStorage.setItem("scrollPosition", this.myRef.current!.scrollTop.toString());
       };

  <SearchListWrapper className="TestSL" ref={this.myRef}  onScroll={this.handleScrollPosition} >
 <StyledLink onClick={ () =>{ this.onClickResult(); } } >
 </StyledLink>
</SearchListWrapper>

On click of StyledLink the new page is loaded. When I go back(with browser's back button) from the newly loaded page, I want to restore the position of scroll on SearchListWrapper .

2
  • 1
    You might want create ref const ref = useRef<HTMLDivElement>(); and to pass it to <SearchListWrapper ref={ref} and then process onScroll event accessing ref.current.scrollX and ref.current.scrollY. To do that SearchListWrapper must be defined as React.forwardRef(...) component Commented Nov 22, 2022 at 16:42
  • Please try to further describe what the goal is. I am not sure if you want element position relative to the page, scroll position inside of that div (inner scroll of that div), or just simple page scroll. Commented Nov 22, 2022 at 16:58

2 Answers 2

0

If you want scroll position of the page, what you are doing should work, but remind you, that you won't get scroll until you scroll on entire page height.

If you want to get position of the element relative to the page you can you this method: getBoundingClientRect().top. In JavaScript this is used with followingly:

document.getElementById(element).getBoundingClientRect().top;

But as you are in React you are not supposed to be refering to elements in this way. You should use useRef that you give to the element that you want the position of and then use

yourRef.current.getBoundingClientRect().top

So example of this is:

export default function App() {
  const divRef = React.useRef();
  if (divRef) {
    console.log(divRef.current.getBoundingClientRect().top);
  }
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2> Start editing to see some magic happen!</h2>
      <div ref={divRef}>Text</div>
    </div>
  );
}

Alternative to this solution to this is here: Get scroll position with Reactjs

Sign up to request clarification or add additional context in comments.

5 Comments

divRef.current.getBoundingClientRect().top value does not change on scrolling of div
@MaithiliKalkar Ill repeat what I said "Please try to further describe what the goal is. I am not sure if you want element position relative to the page, scroll position inside of that div (inner scroll of that div), or just simple page scroll.". I do not have psychic powers
I got the scroll position. My goal is to retain the scroll position on back button.
I don't think your code example shows where "back button" is. All the only information I have are the ones you have provided. Please edit your post to describe the problem and provide minimum yet complete amount of code.
The back button is brower's back button. I have updated the question. Sorry but I am not exactly able to describe what is required in a question
-1

Solution

handleScrollPosition(e){
    const scrollTop = document.getElementById("TestSL").scrollTop;
    //use scrollTop here
}
<SearchListWrapper id="TestSL" onScroll={this.handleScrollPosition} >

4 Comments

This is solution for JavaScript not React. +You are trying to use .getElementById on className
added jsx also with id="TestSL"
But you took away the class that's there most likely for styling, still are addressing elements using document.getElementById rather than using refs.
Ummm..agreed! Your solution looks good.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.