2

I'm trying to get data from an Airtable table using their API and displaying the data in a table. Like this:

import React from 'react';
import { GridComponent, Inject, ColumnsDirective, ColumnDirective, Search, Page } from '@syncfusion/ej2-react-grids';

import { employeesData, employeesGrid } from '../data/dummy';
import { Header } from '../components';
import { getRotatedRectangleCoordinates } from '@syncfusion/ej2-react-charts';

const Users = () => {


  var Airtable = require('airtable');
  var base = new Airtable({ apiKey: 'API-KEY' }).base('APP-ID');
  var listOfUsers = []

  base('Users').select({
    maxRecords: 200,
    view: "Grid view",
    fields: ["Nom complet", "Email"]
  }).firstPage(function (err, records) {
    if (err) { console.error(err); return; }
    records.forEach(function (record) {
      listOfUsers.push(record.fields);
    });
  });

  console.log(listOfUsers)

  const toolbarOptions = ['Search'];

  const editing = { allowDeleting: true, allowEditing: true };

  return (
    <div className="m-2 md:m-10 mt-24 p-2 md:p-10 bg-white rounded-3xl">
      <Header category="Page" title="Users" />
      <GridComponent
        dataSource={listOfUsers}
        width="auto"
        allowPaging
        allowSorting
        pageSettings={{ pageCount: 5 }}
        editSettings={editing}
        toolbar={toolbarOptions}
      >
        <ColumnsDirective>
          {/* eslint-disable-next-line react/jsx-props-no-spreading */}
          {employeesGrid.map((item, index) => <ColumnDirective key={index} {...item} />)}
        </ColumnsDirective>
        <Inject services={[Search, Page]} />

      </GridComponent>
    </div>
  );
};
export default Users;

The data is parsed to listOfUsers().

The problem is that, I think, the page and the table loads before the data gets retrieved, which displays an error on the page :

Error

Polymood
  • 397
  • 3
  • 14

1 Answers1

1

The problem is that you are pushing items in listOfUsers. React needs new instance every time you change something so it can acknowledge this change. What you have to do is:

const [listOfUsers, setListOfUsers] = useState([]);

And later when you are updating listOfUsers with every record

setListIfUsers([...listOfRecords, record.fields]);
Iavor Orlyov
  • 512
  • 1
  • 4
  • 15