Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #3948 - Changing state was also causing change of initial value #3949

Merged
merged 5 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/empty-vans-bathe.md
Original file line number Diff line number Diff line change
@@ -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.
9 changes: 5 additions & 4 deletions packages/formik/src/Formik.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -173,10 +174,10 @@ export function useFormik<Values extends FormikValues = FormikValues>({

const [, setIteration] = React.useState(0);
const stateRef = React.useRef<FormikState<Values>>({
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,
Expand Down
44 changes: 44 additions & 0 deletions packages/formik/test/Formik.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ const InitialValues = {
age: 30,
};

const InitialValuesWithNestedObject = {
content: {
items: [
{
cards: [
{
desc: 'Initial Desc',
},
],
},
],
},
};

function renderFormik<V extends FormikValues = Values>(
props?: Partial<FormikConfig<V>>
) {
Expand Down Expand Up @@ -1454,4 +1468,34 @@ describe('<Formik>', () => {

expect(innerRef.current).toEqual(getProps());
});

it('should not modify original initialValues object', () => {
render(
<Formik initialValues={InitialValuesWithNestedObject} onSubmit={noop}>
{formikProps => (
<input
data-testid="desc-input"
value={formikProps.values.content.items[0].cards[0].desc}
onChange={e => {
const copy = { ...formikProps.values.content };
copy.items[0].cards[0].desc = e.target.value;
formikProps.setValues({
...formikProps.values,
content: copy,
});
}}
/>
)}
</Formik>
);
const input = screen.getByTestId('desc-input');

fireEvent.change(input, {
target: {
value: 'New Value',
},
});

expect(InitialValuesWithNestedObject.content.items[0].cards[0].desc).toEqual('Initial Desc');
});
});
Loading