4

I am trying to implement the download feature using React Table, the react-csv package, and TypeScript.

I am trying to create and use a reference for the table component using createRef() however it is throwing the following exception

"Property 'getResolvedState' does not exist on type 'RefObject'" error.

My code is as follows:

import {CSVLink} from "react-csv";
import * as React from 'react';
import ReactTable from 'react-table';

export default class Download extends React.Component<{},{}> {

  private reactTable: React.RefObject<HTMLInputElement>;
  constructor(props:any){
    super(props);
    this.state={} // some state object with data for table
    this.download = this.download.bind(this);
    this.reactTable = React.createRef();
  }

   download(event: any)
   {
    const records =this.reactTable.getResolvedState().sortedData; //ERROR saying getResolved state does not exist
     //Download logic
   }

    render()
    {
       return(
       <React.Fragment>
       <button onClick={this.download}>Download</button>
       <ReactTable 
           data={data} //data object
           columns={columns}  //column config object
           ref={this.reactTable}
       />
     </React.Fragment>
    }
}

Any help would be appreciated
Dacre Denny
  • 29,664
  • 5
  • 45
  • 65
joy08
  • 9,004
  • 8
  • 38
  • 73

1 Answers1

2

You should find that the issue is resolved by:

  1. revising the way you relate the reactTable ref to your <ReactTable /> component as documented here, and
  2. accessing getResolvedState() from the current field of your reactTable ref

Also, consider wrapping both of your rendered elements with a fragment to ensure correct rendering behavior:

/* Note that the reference type should not be HTMLInputElement */
private reactTable: React.RefObject<any>;

constructor(props:any){
  super(props);
  this.state={};
  this.download = this.download.bind(this);
  this.reactTable = React.createRef();
}

download(event: any)
{
   /* Access the current field of your reactTable ref */
   const reactTable = this.reactTable.current;

   /* Access sortedData from getResolvedState() */
   const records = reactTable.getResolvedState().sortedData;

   // shortedData should be in records
}

render()
{
   /* Ensure these are correctly defined */
   const columns = ...;
   const data = ...;

   /* Enclose both elements in fragment, and pass reactTable ref directly */
   return <React.Fragment>
   <button onClick={this.download}>Download</button>
   <ReactTable 
       data={data}
       columns={columns}
       ref={ this.reactTable } />
   </React.Fragment>
}

Hope that helps!

Dacre Denny
  • 29,664
  • 5
  • 45
  • 65