reactjs - Adding debounce to react hook form
问题描述
I have the following controlled component set up using react-hook-forms.
<Controller
control={control}
name={"test"}
render={({ field: { onChange, value, name } }) => (
<Dropdown
name={name}
value={value}
handleChange={onChange}
options={foodCategories()}
/>
)}
/>
I want to call a debounce and and I tried doing the following:
handleChange={debounce(onChange, 500)}
but I keep getting errors throw:
This synthetic event is reused for performance reasons, Objects are not valid as a React child (found: object with keys {dispatchConfig, _targetInst, nativeEvent, type, target, currentTarget, eventPhase, bubbles, cancelable,
How can I call debounce on a controlled react hook form component?
解决方案
这可以通过以下实现来实现;
<Controller
name={fieldName}
rules={{ required: `errors.${fieldName}.required` }}
render={({ field, fieldState: { error } }) => (
<input
type="text"
error={error && t(`${error.message}`)}
onChange={debounce((e) => field.onChange(e), 50)}
defaultValue={field.value}
/>
)}
/>
推荐阅读
- python - Predict next numbers
- jquery - 如何防止具有两个下拉列表的表的第一行在更改时动态更改第二个下拉列表的值?
- jsf - 根据 p:inputText 的 null 值启用/禁用
- php - How do I put my php script on my WAN IP address as it's currently on my local host?
- javascript - 将来自 GET 请求的数据存储在数组中
- html - Webpage with 4 digit code input ->Restful transfer, with iBeacon Support
- r - How can I prevent R from automatically converting æ to a?
- reactjs - add image from value
- facebook - Instagram 基本显示 API 分页
- mysql - 多台机器,我的笔记本电脑(Master Machine)上的一台mysql服务器,一个或多个数据库