2

I am using react-table library along with axios to make the get call from api. my use efft looks like this.

 const [data, setData] = useState([]);
      useEffect(() => {
        (async () => {
          const result = await axios("http://site");
          setData(result.data)
        })();
      }, []);

  return (

    <div className="App">
      <Table columns={columns} data={data} />
    </div>
  );

I am however getting an error when trying to render the table. The error that I am getting seems to be coming from my Table.js file

Error: Table

src/Table.js:8
   5 | 
   6 | const [filterInput, setFilterInput] = useState("");
   7 | // Use the state and functions returned from useTable to build your UI
>  8 | const {
     | ^   9 |   getTableProps,
  10 |   getTableBodyProps,
  11 |   headerGroups,

I am new to using react-table, any ideas why? I did console.log the result and it is coming in as an object.

4
  • You seem to be attempting to render your data before it actually arrives. You need to do something, like return data && (.../* the rest of your JSX */ to check whether the data is available before render. Commented Sep 17, 2020 at 12:53
  • @YevgenGorbunkov I think you may be right, that passed by me the first time. Whats a good way to do that in axios? Commented Sep 17, 2020 at 12:58
  • @YevgenGorbunkov I check and the data is been set. Commented Sep 17, 2020 at 14:29
  • It's not about whether you get data or not, it's about when you get the data. Commented Sep 17, 2020 at 17:50

1 Answer 1

1

You need some kind of a 'Loading' component which will render until your axios is complete and you have data to render. You could use it like this:

return !data.length ? (<div>Loading..</div>) : (<div className="App">
  <Table columns={columns} data={data} />
</div>)
Sign up to request clarification or add additional context in comments.

1 Comment

it wasnt this, just checked and I am geting the data in the console.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.