reactjs - react-hook-form isDirty 对我来说似乎很奇怪
问题描述
今天,我开始使用 react-hook-form 并且isDirty变量对我来说似乎很奇怪。尽管仅将焦点放在任何输入元素上,但它始终是正确的。
我希望 isDirty 只有在输入元素的值发生变化时才应该为真。这在反应钩子形式中是正常的吗?
// I had to make workaround like this. but I am not sure if this is normal for everybody.
const closeForm = () => {
const { dirtyFields } = form.formState
const isReallyDirty = Object.keys(dirtyFields).length > 0
if (isReallyDirty) {
if (window.confirm("Discard the changes?")) {
dispatch(closeItem())
}
} else {
dispatch(closeItem())
}
}
更新:我认为这是 react-hook-form 的错误? react-hook-form 版本 6.11.0
这仅在使用 React.forwardRef 时发生。
const TextareaBox = ({ ref, ...props }) => {
const { errors, name } = props
const { required, ...restProps } = props
return (
<Row>
<Label {...props} columnSize={2} />
<Col lg={10}>
<textarea id={name} {...restProps} maxLength="200" rows="3" ref={ref} />
<ErrorMessage className="errorMessage" errors={errors} name={name} as="p" />
</Col>
</Row>
)
}
const TextareaBox = React.forwardRef((props, ref) => {
const { errors, name } = props
const { required, ...restProps } = props
return (
<Row>
<Label {...props} columnSize={2} />
<Col lg={10}>
<textarea id={name} {...restProps} maxLength="200" rows="3" ref={ref} />
<ErrorMessage className="errorMessage" errors={errors} name={name} as="p" />
</Col>
</Row>
)
})
解决方案
推荐阅读
- python - 如何在除日期列之外的所有行都是 NaN 的地方删除?
- wordpress - 为某些页面 ID 禁用古腾堡编辑器
- javascript - 我的 REACT 组件类中的状态未更新
- shader - 像素着色器中的碰撞检查
- javascript - How to place many divs horizontally automatically?
- git - --mirror 在尝试删除 git 历史记录中的文件时不能与 refspecs 结合使用
- javascript - 如何在 Google Apps 脚本中查找和替换值?
- php - 如何在 WooCommerce 中显示自定义产品描述页面?
- html - 如何在 DIV 的左右创建波浪形边框
- sonarqube - 如何配置 SonarAnalyzer.CSharp S1451 使用 .editorconfig 时跟踪缺少版权和许可标头