javascript - 反应选择 - 缓冲 onChange 事件
问题描述
我需要暂停 react-select 组件的 onChange ,直到数据加载到选项中。怎么可能?
const handleSearch = () => {
// call data to redux and add to function getOptions (THIS WORK OK - this doesn't interest me)
}
const handleSelect = () => {
// need pause event while handleSearch is completed
// **how to do this**
}
<Select
...
isLoading={isSearching}
onInputchange={handleSearch}
onChange={handleSelect}
options={getOptions}
...
/>
解决方案
创建新状态以检测句柄搜索是否完成。
const [isHandleSearchComplete, markHandleSearchAsComplete] = useState(false);
const handleSearch = () => {
getOptions().then((res) => {
// do something here
markHandleSearchAsComplete(true)
})
}
const handleSelect = () => {
// if handle search is complete then stop the process
if (isHandleSearchComplete) {
return
}
// otherwise, do something
}
推荐阅读
- python - GPU 上的 Google colab 教程:“OSError:libcudart.so.10.2:无法打开共享对象文件:没有这样的文件或目录”
- swift - UIKit中的标签栏控制器问题
- arrays - (wx)Maxima:作业
- javascript - 由 div 固定标题组成的表格,但在 x 轴上没有与表格一起滚动
- reactjs - TypeError:无法读取未定义的属性(读取“img”)反应
- mysql - 跨 100 个不同表的 MySQL 查询。模式匹配表名称
- arrays - 是否有散列函数将元素对映射到数组中的索引?
- powershell - RavenDB 安装问题 System.Net.Sockets.SocketException (10013)
- c# - 添加局部视图以按计划查看
- python - 尝试在 Linux 上下载 Python 包时出错