From 16b967306d962898a9030ca15db356edcc997e18 Mon Sep 17 00:00:00 2001 From: DeveloperRaj <40798951+DeveloperRaj@users.noreply.github.com> Date: Tue, 30 Jan 2024 17:51:37 +0530 Subject: [PATCH 1/3] Fix #3948 - Changing state was also causing change of initial value --- packages/formik/src/Formik.tsx | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/formik/src/Formik.tsx b/packages/formik/src/Formik.tsx index cd17adcd4..c72ac068f 100755 --- a/packages/formik/src/Formik.tsx +++ b/packages/formik/src/Formik.tsx @@ -29,6 +29,7 @@ import { setIn, setNestedObjectValues, } from './utils'; +import cloneDeep from 'lodash/cloneDeep'; type FormikMessage = | { type: 'SUBMIT_ATTEMPT' } @@ -173,10 +174,10 @@ export function useFormik({ const [, setIteration] = React.useState(0); const stateRef = React.useRef>({ - values: props.initialValues, - errors: props.initialErrors || emptyErrors, - touched: props.initialTouched || emptyTouched, - status: props.initialStatus, + values: cloneDeep(props.initialValues), + errors: cloneDeep(props.initialErrors) || emptyErrors, + touched: cloneDeep(props.initialTouched) || emptyTouched, + status: cloneDeep(props.initialStatus), isSubmitting: false, isValidating: false, submitCount: 0, From 6d7d0942f75c1d51dd17097e90eee3f49be4edfb Mon Sep 17 00:00:00 2001 From: DeveloperRaj Date: Wed, 10 Apr 2024 20:36:45 +0530 Subject: [PATCH 2/3] Fix review comment and added changeset --- .changeset/empty-vans-bathe.md | 5 +++++ packages/formik/src/Formik.tsx | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 .changeset/empty-vans-bathe.md diff --git a/.changeset/empty-vans-bathe.md b/.changeset/empty-vans-bathe.md new file mode 100644 index 000000000..adfff99c5 --- /dev/null +++ b/.changeset/empty-vans-bathe.md @@ -0,0 +1,5 @@ +--- +'formik': patch +--- + +Changing the state inside formik was changing reference of initialValues provided via props, deep cloning the initialvalues will fix it. diff --git a/packages/formik/src/Formik.tsx b/packages/formik/src/Formik.tsx index c72ac068f..80d868665 100755 --- a/packages/formik/src/Formik.tsx +++ b/packages/formik/src/Formik.tsx @@ -1,5 +1,6 @@ import deepmerge from 'deepmerge'; import isPlainObject from 'lodash/isPlainObject'; +import cloneDeep from 'lodash/cloneDeep'; import * as React from 'react'; import isEqual from 'react-fast-compare'; import invariant from 'tiny-warning'; @@ -29,7 +30,6 @@ import { setIn, setNestedObjectValues, } from './utils'; -import cloneDeep from 'lodash/cloneDeep'; type FormikMessage = | { type: 'SUBMIT_ATTEMPT' } From d8fde1ba332cd5bca5a59fe30ce807782ecba087 Mon Sep 17 00:00:00 2001 From: DeveloperRaj Date: Wed, 10 Apr 2024 23:25:44 +0530 Subject: [PATCH 3/3] Updated test to check that initial values are not being modified --- packages/formik/test/Formik.test.tsx | 44 ++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/packages/formik/test/Formik.test.tsx b/packages/formik/test/Formik.test.tsx index 2acf332dd..98b6ef3c9 100644 --- a/packages/formik/test/Formik.test.tsx +++ b/packages/formik/test/Formik.test.tsx @@ -61,6 +61,20 @@ const InitialValues = { age: 30, }; +const InitialValuesWithNestedObject = { + content: { + items: [ + { + cards: [ + { + desc: 'Initial Desc', + }, + ], + }, + ], + }, +}; + function renderFormik( props?: Partial> ) { @@ -1454,4 +1468,34 @@ describe('', () => { expect(innerRef.current).toEqual(getProps()); }); + + it('should not modify original initialValues object', () => { + render( + + {formikProps => ( + { + const copy = { ...formikProps.values.content }; + copy.items[0].cards[0].desc = e.target.value; + formikProps.setValues({ + ...formikProps.values, + content: copy, + }); + }} + /> + )} + + ); + const input = screen.getByTestId('desc-input'); + + fireEvent.change(input, { + target: { + value: 'New Value', + }, + }); + + expect(InitialValuesWithNestedObject.content.items[0].cards[0].desc).toEqual('Initial Desc'); + }); });