reactjs - React-Select:如何在加载选项时显示“正在加载...”
问题描述
我正在尝试为我的 WordPress 块组件使用“React-Select”来选择帖子类别。
我想要的是在加载类别时显示“正在加载...”,然后在加载完成时显示类别列表或“未找到类别”。
使用我的代码,它在加载时显示“未找到类别”,然后显示类别列表。
export default class Categories extends Component {
constructor() {
super( ...arguments );
this.state = {cats: []};
}
componentDidMount () {
return apiFetch({path: '/myoriginal-blocks/v1/categories'})
.then(data => {
this.setState({cats:data});
})
}
onChangeCategories = newCategories => {
this.props.setAttributes({ category: newCategories == null ? [] : newCategories });
}
render() {
const { attributes } = this.props;
const { category } = attributes;
return (
<>
<div>
<label>Categories</label>
<Select
isMulti
isClearable
placeholder='Search categories...'
onChange={ this.onChangeCategories }
options={ this.state.cats }
value={ category && category }
noOptionsMessage={() => 'No Categories Found'}
/>
</div>
</>
);
}
}
或者也许我必须使用“Async-Select”而不是“React-Select”,但我不能很好地理解它的文档。
https://react-select.com/async
希望有人帮助我。谢谢你。
解决方案
你可以isLoading
支持 react-select 组件。
在您的状态中添加一个属性来管理加载
this.state = {cats: [], isLoading: true};
然后设置 isLoading false
return apiFetch({ path: "/myoriginal-blocks/v1/categories" }).then(
(data) => {
this.setState({ cats: data, isLoading: false });
}
);
并在 Select 组件中传递 isLoading 道具
<Select
isMulti
isClearable
placeholder='Search categories...'
onChange={ this.onChangeCategories }
options={ this.state.cats }
value={ category && category }
noOptionsMessage={() => 'No Categories Found'}
isLoading={this.state.isLoading}
/>
推荐阅读
- python-3.x - tf.keras.optimizers.Adam 与 Tensorflow 2.0.beta 中的 tf.estimator 模型正在崩溃
- angular - 将参数传递给弹出对话框
- python - 使用 LSTM 进行样本外预测
- javascript - 智能手机键盘弄乱了页面布局
- python - 将多张图像与一张图像进行比较
- python - 在最终图像上应用轮廓蒙版
- swift - Firestore 并行查询持续时间随着查询数量的增加而增加(而且不应该)
- c++ - 仅在场景中的一个对象上设置纹理
- python - Python-OBD 大多数命令不支持
- sql-server - SQL Server 触发器插入/更新特定列