【发布时间】:2019-10-07 19:52:58
【问题描述】:
我正在使用 react 和 react-select 并尝试从 api 获取选项。一切都对我有用,但我想在数据来自 api 时显示将文本加载到 react-select 中。
这是我的 react-select 组件:
<Select
onChange={this.getSelectedOption}
onFocus={this.getData}
options={options}
value={selectedOption}
styles={customStyles}
placeholder="Select...."
/>
在 onFocus 中,我通过 redux 操作进行 api 调用,并使用来自 redux reducer 的响应设置状态。
getAvailableDisplays() {
this.props.getData(); /// redux action dispatch
if (this.props.data.items) {
this.setState({ this.state.options: this.props.data.items }); }
}
通过这种方式一切正常,但是在请求完成时没有文本选项..
我在这里看到了 https://react-select.com/async 的东西,但我不明白在哪里进行 api 调用。
或者如何在 api 调用完成并填充状态时在 select 中写入加载
【问题讨论】:
-
你能分享一个密码箱吗?根据您提供的信息,目前尚不清楚您是如何进行 api 调用的。
-
如何进行 api 调用? :) 我正在使用 axios 获取数据响应并将其设置为组件状态
标签: javascript reactjs react-select