react-bootstrap-typeahead - 使用时我不能同时使用 onSearch 和 onInputChange
问题描述
所以正如我在标题中所说,我不能同时使用 onSearch 和 onInputChange。如果我尝试,onSearch 会被忽略。你能告诉我为什么或者有替代 onInputChange 的方法吗?我在表单中使用 AsyncTypeahead,因此单击按钮时需要这些值。
const [from, setFrom] = useState('');
const onSearchStart = (query) => {
setTypeAhead({
...typeAhead,
startIsLoading: true
})
setFrom(query)
getStations(query).then(r => {
setTypeAhead({
...typeAhead,
startIsLoading: false,
startOptions: r || []
})
})
}
return(
<AsyncTypeahead
id={"from"}
isLoading={typeAhead.startIsLoading}
options={typeAhead.startOptions}
onInputChange={setFrom}
onSearch={onSearchStart}
/>
)
这不是全部代码,但它显示了导致错误的所有内容
解决方案
作为一种解决方法,您可以使用它useRef
来不触发重新渲染。这是代码:
const fromRef = useRef('');
const onSearchStart = (query) => {
...
}
return(
<AsyncTypeahead
...
onInputChange={(text) => {
fromRef.current = text;
}}
onSearch={onSearchStart}
/>
)
推荐阅读
- java - 为什么 AWS S3 JDK Java 会给我检查您的密钥和签名方法?
- typescript - 打字稿超类型
- android - 将从 DatePickerDialog 获取的日期格式转换为 yyyy-MM-dd
- html - html创建按钮时Angular5调用一个函数
- php - INSERT INTO... ON DUPLICATE KEY UPDATE 不更新 PHP 和 mySQL 中的重复值
- android - 添加依赖项时出现编译错误:com.google.firebase:firebase-core
- azure - 在 azure 中托管后在路由级别刷新时,angular 7 应用程序无法正常工作
- amazon-web-services - 通过 aws cli 删除多个文件不起作用
- javascript - 如何对每行有 3 个值的数组进行排序?
- wordpress - 在 woocomerce rest api 中使用 Coupon_lines 创建购物车