Reactjs 如何在React with Typescript上对组合框项目进行排序?
我有一个项目,项目中有一个年度组合框。我将把这一年分类为asc到desc或desc到asc 我有一个表单utils,我在组件中调用utils。试试这种方法Reactjs 如何在React with Typescript上对组合框项目进行排序?,reactjs,typescript,react-native,Reactjs,Typescript,React Native,我有一个项目,项目中有一个年度组合框。我将把这一年分类为asc到desc或desc到asc 我有一个表单utils,我在组件中调用utils。试试这种方法 import React, { Component } from "react"; import Select from "react-select"; function compare(a, b) { return a.label > b.label ? 1 : b.label >
import React, { Component } from "react";
import Select from "react-select";
function compare(a, b) {
return a.label > b.label ? 1 : b.label > a.label ? -1 : 0;
}
class App extends Component {
constructor(props) {
super(props);
this.state = {
options: [
{
label: 1940,
value: 1
},
{
label: 1945,
value: 2
},
{
label: 1942,
value: 3
},
{
label: 1941,
value: 4
},
{
label: 1946,
value: 5
}
]
};
}
render() {
return (
<Select
//isMulti
hideSelectedOptions={false}
options={this.state.options.sort(compare)}
/>
);
}
}
这里有一个除非你提供足够的问题信息,否则没有人能帮助你。至少您可以包含您正在使用的代码。