reactjs - 如何使用 Typescript 对 React 上的组合框项目进行排序?
问题描述
我有一个项目,在项目中有一年的组合框。我将年份排序(asc to desc 或 desc to 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 > 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)}
/>
);
}
}
推荐阅读
- java - getIntValue 如何返回 Integer 值?有什么循序渐进的解释吗?
- java - Java无法在java中执行CMD命令
- xamarin.forms - Xamarin 表单中的 SignalR 我配置如下,但这些 NewJob、UpdateJob 没有调用
- javascript - 有没有解决此 WEBRTC 错误的解决方案
- python - 如何同时下载两个用户的推文?
- node.js - NodeJS pkg如何指定主函数
- reactjs - React 设置:Git 子模块被 Husky - 6.0.0 忽略
- python - 使用 Selenium 和 Python 抓取 Flipkart 移动设备的详细信息
- java - 用Java实现CustomList的List接口
- npm - npm install 导致 core-js 安装后错误,Plesk 环境中的 Nuxt 应用程序