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

353
Visualizações
React Table set Header onClick-Function without disable sort-Function

How can I set an onClick-Function to my table-header without removing the sort function (useSortBy) which will be executed by clicking the Header. Here is my code:

function renderTableHeader() {
  const headers = headerGroups.map(headerGroup => {
    return (
      <tr {...headerGroup.getHeaderGroupProps()}>
        {headerGroup.headers.map(column => (
          <th
            {...column.getHeaderProps(
              column.sortable && column.getSortByToggleProps()
            )}
          >
            {(column.render("Header")}
            <span>
              <i className={determineSortState(column)} />
            </span>
          </th>
        ))}
      </tr>
    );
  });
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

react-table sets up an onClick handler on the <th>element by this expression: {...column.getHeaderProps(column.getSortByToggleProps())}

If you put your own onClick handler before it, it will be overridden. If you put it behind it, it will override the handler from react-table, which will break the sorting.

So you need to trigger the react-table handler manually in your override like this:

            <th
              {...column.getHeaderProps(column.getSortByToggleProps())}
              onClick={(e) => {
                //trigger the react-table header onClick manually
                  column
                  .getHeaderProps(column.getSortByToggleProps())
                  .onClick(e);

                //your onclick functionallity goes in here
                console.log("my on click");
              }}
            >
about 4 years ago · Juan Pablo Isaza Relatório

0

For v6 or below as per Docs : https://github.com/TanStack/react-table/tree/v6, we should maintain state for sortOptions to remember the sorting even after calling onClick Func of any methods in ReactTable.

TO achieve that:-

1.State value sortOptions should be updated using newSorted parameter from onSortedChange method, like this: onSortedChange={(newSorted) => {setSortOptions(newSorted)}}

2.State value sortOptions should then be passed as value to sorted attribute in ReactTable sorted={sortOptions}

const [sortOptions, setSortOptions] = useState();
<ReactTable 
sorted={sortOptions}
onSortedChange={(newSorted) => {setSortOptions(newSorted)}}
getTrProps={(state, rowInfo) => {
        if (rowInfo && rowInfo.row) {
          return {
            onClick: (e) => {
              // Do Something...for onCLick Event
            }
          }
        }}}
data={data}
columns={columns}
/>
about 4 years ago · Juan Pablo Isaza Relatório
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