React-Table:在组件 ref 上调用 'getResolvedState()' 抛出异常
React-Table: exception thrown calling 'getResolvedState()' on component ref
我正在尝试使用 React Table、react-csv
包和 TypeScript 实现下载功能。
我正在尝试使用 createRef()
创建和使用 table 组件的引用,但是它抛出以下异常
"Property 'getResolvedState' does not exist on type 'RefObject'" error.
我的代码如下:
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
您应该发现问题已通过以下方式解决:
- 修改将
reactTable
ref 与 <ReactTable />
组件关联的方式 as documented here,以及
- 从你的 reactTable 引用的
current
字段访问 getResolvedState()
此外,考虑包装两个渲染元素 with a fragment 以确保正确的渲染行为:
/* 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>
}
希望对您有所帮助!
我正在尝试使用 React Table、react-csv
包和 TypeScript 实现下载功能。
我正在尝试使用 createRef()
创建和使用 table 组件的引用,但是它抛出以下异常
"Property 'getResolvedState' does not exist on type 'RefObject'" error.
我的代码如下:
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
您应该发现问题已通过以下方式解决:
- 修改将
reactTable
ref 与<ReactTable />
组件关联的方式 as documented here,以及 - 从你的 reactTable 引用的
current
字段访问getResolvedState()
此外,考虑包装两个渲染元素 with a fragment 以确保正确的渲染行为:
/* 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>
}
希望对您有所帮助!