我正在尝试使用通过API检索的列表对表进行排序。我在React + Typescript中这样做。我正面临类型错误。请帮帮我。下面是我的代码。所以,我面临着错误 bind(this)语句。
const Row = ({name, type, model, mode, fuel_type, color , length , height , width, mass, tag}) => (
<div className="row">
<div>{name}</div>
<div>{type}</div>
<div>{color}</div>
</div>
);
constructor(props); {
this.state = {
data:
};
this.compareBy = this.compareBy.bind(this);
this.sortBy = this.sortBy.bind(this);
}
function compareBy(key) {
return function (a, b) {
if (a[key] < b[key]) return -1;
if (a[key] > b[key]) return 1;
return 0;
};
}
function sortBy(key) {
let vehicle = [...this.state.data];
vehicle.sort(this.compareBy(key));
this.setState({data: });
}
const rows = this.state.data.map( (rowData) => <Row {...rowData} />);
inside return in <Table> tag am defining
<th><button onClick={() => this.sortBy('name')}>Name</button></th>