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

179
Vistas
How to save expand/collapse state of react-table in local storage?

I have a hook for saving state in local storage:

  const [expandingState, setExpandingState] = useLocalStorageState<Record<string, boolean>>(
    'expandingState',
    {},
  );

and after user is clicking on expand/collapse element I manipulate state value using setExpandingState like this:

  const handleExpandCollapse = (row: UseTableRowProps<TableTreeProps>): void => {
    setExpandingState(prevState => {
      if (!prevState[row.id]) {
        return {
          ...prevState,
          [row.id]: true,
        };
      } else {
        const newState = { ...prevState };

        delete newState[row.id];

        return newState;
      }
    });
  };

More about particular react-table API: https://react-table.tanstack.com/docs/api/useExpanded

I use row.id to bind the clicked field and expandingState values, but with this approach if we add or remove one more element to the table, then the row.id of each row of the table is shifted and we have incorrect values in the state and, as a result, not correctly display expanded and collapsed table elements...

about 4 years ago · Juan Pablo Isaza
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