Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

261
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda