reactjs - 在 react-select 上使用 isClearable 时事件为空
问题描述
我正在通过 react-select Select 组件作为 Material-UI InputBase 组件中的 InputComponent。我已经成功地能够从选项中填充值,但是,我无法使用isClearable
.
当 isClearable 被触发时, null 被传递给handleChange(event)
函数,我希望有一种方法可以强制对象通过,以防止 null 创建错误。
InputBase 中的 handleChange 函数具有var element = event.target || inputRef.current
. 由于 event 为空,它甚至没有到达将包含所需对象的 inputRef。
让它作为一个不受控制的组件工作会很好。
我创建了一个代码框来说明问题:https ://codesandbox.io/s/morning-feather-l7xqf
解决方案
您可以提供您的自定义onChange()
来捕捉null
并传递您自己的价值:
// Deconstruct from otherProps
const SelectWrapper = ({ inputRef, onChange, ...otherProps }) => {
function handleChange(event) {
// Overwrite the event with your own object if it doesn't exist
if (!event) {
event = {
target: inputRef,
value: '',
};
}
onChange(event);
}
return (
// Pass in the custom handleChange
<Select styles={customStyle} isClearable ref={inputRef} onChange={handleChange} {...otherProps} />
);
};
推荐阅读
- mysql - SQL 打印在一行中
- c# - 在 Windows 10 上访问区域附加设置的数据
- php - 如何抑制执行的 Composer 脚本回显命令?
- bash - 使用过滤器获取要排列的文件列表
- ssl - gRPC SSL 不存在主题替代名称
- forms - 为什么 Flutter 会重新渲染包含带有自己键的 TextFormField 的小部件?
- int - 为什么长生不老药指南说在宏定义中使用引用和取消引用
- javascript - How to reformat form submit url that has GET method on the fly
- excel - 将行值从一张表复制到另一张表以获取 Excel 中匹配的记录
- sql - Oracle 滚动 LEAD 计算