Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

257
Views
How to maintain and restore the scroll position using sessionStorage in react.js?

How to maintain and restore the scroll position using sessionStorage in React.js?

I geted horizontal scroll position using "querySelector" and I try useEffect.. but not working.

How do I solve this problem?

/

Here's my code

export const TopNavigation = () => {
  const [selected, setSelected] = React.useState<string>("");

  const scrollX = document.querySelector(
    ".react-horizontal-scrolling-menu--scroll-container "
  )?.scrollLeft;
  console.log(scrollX);

    useEffect(() => {
      sessionStorage.setItem("scrollX", JSON.stringify(xPosition));
    }, []);
    
    useEffect(() => {
      if (selected === window.location.pathname) {
        sessionStorage.getItem("scrollX");
      }
    }, []);

  const handleItemClick =
    (itemId: string) =>
    ({ getItemById, scrollToItem }: scrollVisibilityApiType) => {
      setSelected(selected !== itemId ? itemId : "");
      scrollToItem(getItemById(itemId), "smooth", "center", "nearest");
    };

  return (
    <div id="TopNavigation">
      <ScrollMenu
        onMouseUp={({
            getItemById,
            scrollToItem,
            visibleItems,
          }: scrollVisibilityApiType) =>
          () => {
            const { center } = getItemsPos(visibleItems);
            scrollToItem(getItemById(center), "smooth", "center");
          }}
        options={{ throttle: 0 }} 
      >
        {TOP_NAV_PATH_LIST.map((arr, idx) => (
          <Menu
            title={arr.name}
            itemId={arr.name}
            path={arr.path}
            key={idx}
            onClick={handleItemClick(arr.name)}
            selected={arr.path === selected}
          />
        ))}
      </ScrollMenu>
    </div>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The empty dependancy array of your use effect saves the position when the component mounts. At this point, the scroll position will be 0. Change the code to:

  useEffect(() => {
    return () => {
      const scrollX = document.querySelector(".react-horizontal-scrolling-menu--scroll-container")?.scrollLeft;
      sessionStorage.setItem("scrollX", JSON.stringify(xPosition));
    };
  }, []);

This will save the position when the component unmounts.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!