Skip to content

Commit

Permalink
perf(FieldArray): add shouldComponentUpdate to cut down on unnecessar…
Browse files Browse the repository at this point in the history
…y renders (#3784)

* revert types

* chore: add changeset

---------

Co-authored-by: Jason Williams <jwilliams720@bloomberg.net>
  • Loading branch information
quantizor and Jason Williams committed May 26, 2023
1 parent bc9cb28 commit 22e236e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-kangaroos-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'formik': patch
---

Improve performance of the `FieldArray` component by adding a `shouldComponentUpdate` check; this should help avoid unnecessary re-renders which may affect the performance of a form.
22 changes: 22 additions & 0 deletions packages/formik/src/FieldArray.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export type FieldArrayConfig = {
name: string;
/** Should field array validate the form AFTER array updates/changes? */
validateOnChange?: boolean;
/** Override FieldArray's default shouldComponentUpdate */
shouldUpdate?: (nextProps: {}, props: {}) => boolean;
} & SharedRenderProps<FieldArrayRenderProps>;
export interface ArrayHelpers {
/** Imperatively add a value to the end of an array */
Expand Down Expand Up @@ -153,6 +155,26 @@ class FieldArrayInner<Values = {}> extends React.Component<
this.pop = this.pop.bind(this) as any;
}

shouldComponentUpdate(props: any) {
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;
}
}

componentDidUpdate(
prevProps: FieldArrayConfig & { formik: FormikContextType<Values> }
) {
Expand Down

1 comment on commit 22e236e

@vercel
Copy link

@vercel vercel bot commented on 22e236e May 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

formik-docs – ./website

formik-docs-jared.vercel.app
formik.org
www.formik.org
formik-docs.vercel.app
formik-docs-git-master-jared.vercel.app

Please sign in to comment.