Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

178
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda