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

284
Vistas
react-table not rendering data in the initial load and after page refresh

I am trying to render table data from API using fetch, following is the code snippet, table display data disappears if navigate to a different page or refresh the page. Any help is appreciated.

Ref Video: https://watch.screencastify.com/v/EwWkC48SDpulXvRltTdS

import { useState, useMemo, useEffect } from "react";
import { useTable } from "react-table";

const TableData = () => {
  const [columns, setColumns] = useState([]);
  const [data, setData] = useState([]);
  const tableInstance = useTable({ columns, data });
  const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow } =
    tableInstance;

  useEffect(() => {
    async function fetchData() {
      try {
        // Fetch columns
        let colArr = [];
        let responseCol = await fetch("/api/columns");
        responseCol = await responseCol.json();
        responseCol.forEach((record) => {
          let recordObject = {
            Header: `${record.label}`,
            accessor: `${record.value}`,
          };
          colArr.push(recordObject);
        });
        setColumns(colArr);
        // Fetch Rows
        let rowArr = [];
        let rowObj = {};
        let responseRow = await fetch("/api/rows");
        responseRow = await responseRow.json();
        rowArr.length = 0;
        responseRow.records.forEach((record) => {
          columns.forEach((column) => {
            rowObj[`${column.accessor}`] = `${record[column.accessor]}`;
          });
          console.log("RowObj ", rowObj);
          rowArr.push(rowObj);
          rowObj = {};
          console.log("RowArr ", rowArr);
        });
        setData(rowArr);
      } catch (e) {
        console.log(e.message);
      }
    }
    fetchData();
  }, []);

  return (Rendering table)

Columns & Rows are fetched from nextjs api, please let me know for more information.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I simply added loading state variable and used in useeffect to solve it.

useEffect(() => {
    async function fetchData() {
      try {
        //Fetch Columns
        let responseCol = await fetch("/api/columns");
        responseCol = await responseCol.json();
        setOptions(responseCol);
        // Fetch Rows
        let rowArr = [];
        let rowObj = {};
        let responseRow = await fetch("/api/rows");
        responseRow = await responseRow.json();
        rowArr.length = 0;
        responseRow.records.forEach((record) => {
          columns.forEach((column) => {
            rowObj[`${column.accessor}`] = `${record[column.accessor]}`;
          });
          console.log("RowObj ", rowObj);
          rowArr.push(rowObj);
          rowObj = {};
          console.log("RowArr ", rowArr);
        });
        setData(rowArr);
        setIsLoading(false);
      } catch (e) {
        console.log(e.message);
      }
    }
    fetchData();
  }, [isLoading, columns]);
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