javascript - 什么时候不使用 Formik 的 FastField?
问题描述
在 Formik 的FastField的文档中,它有很多你应该在什么时候使用它以及它是如何工作的,但是有没有你不应该使用的情况?
因为如果在某些FastField
情况下更快而在其他情况下没有任何区别,为什么不总是使用FastField
?有没有使用Field
优于的情况FastField
?
解决方案
FastField 有一个 shouldComponentUpdate ,它“关注”仅对其自身道具的更改。
如果您的用例由于任何其他更改而需要重新渲染字段,则不要使用 FastField。即使其他一些道具发生变化,您的组件也不会更新。
此外,根据文档,由于 Formik 字段的重新渲染导致的性能问题可能仅在表单很大(> 30 个字段)的情况下出现。他们建议仅对 >30 个字段使用 FastFields。
shouldComponentUpdate(props: FastFieldInnerProps<Values, Props>) {
if (this.props.shouldUpdate) {
return this.props.shouldUpdate(props, this.props);
} else if (
props.name !== this.props.name ||
getIn(props.formik.values, this.props.name) !==
getIn(this.props.formik.values, this.props.name) ||
getIn(props.formik.errors, this.props.name) !==
getIn(this.props.formik.errors, this.props.name) ||
getIn(props.formik.touched, this.props.name) !==
getIn(this.props.formik.touched, this.props.name) ||
Object.keys(this.props).length !== Object.keys(props).length ||
props.formik.isSubmitting !== this.props.formik.isSubmitting
) {
return true;
} else {
return false;
}
}
推荐阅读
- javascript - 自动循环浏览 GeoMap 中的工具提示
- salesforce - SalesForce DataLoader CLI 无法加载 JDBC 驱动程序
- video - 使用 gstreamer 通过树莓派流式传输 RTSP。无法输出音频
- opencv - OpenCV 网格板姿态检测不正确
- apache-flink - 使用 Python 的 Apache Beam ReadFromKafka 在 Flink 中运行,但没有发布的消息通过
- css - CSS Grid > 如何让侧栏变得流畅?
- docker - 使用 nuget.config 恢复 Jenkins 包
- python - Python-pptx 与 LibreOffice 中的文本框的兼容性
- windows - Mac 和 PC 之间的通用文件夹名称和路径
- spring - Spring Boot 上的 Swagger:文档仅在 localhost 上可见