我尝试使用“React-Select”作为我的 WordPress 块组件来选择帖子类别。
我想要的是在加载类别时显示“正在加载...”,然后在加载完成时显示类别列表或“未找到类别”。
使用我的代码,它在加载时显示“未找到类别”,然后显示类别列表。
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 } …Run Code Online (Sandbox Code Playgroud)