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
You should find that the issue is resolved by:
reactTable
ref to your <ReactTable />
component as documented here, andgetResolvedState()
from the current
field of your reactTable refAlso, 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!