jaredpalmer / jaredpalmer/formik
Comparing initialValues from enableReinitialize and values in onSubmit
- Dominant language
- TypeScript
- Stars
- 34.3k
- Forks
- 2.8k
- PR merge metrics
- No merged PRs in 30d
Description
I have a form that uses `enableReinitialize` to hydrate my `initialValues` from an api call. I have a use case where I want/need to compare `initialValues` and `values` in `onSubmit` to determine what values to send in a request. I don't want to send values that don't differ from what's in `initialValues`.
` {
console.log("values in onSubmit", values);
// initialValues is undefined below
const { initialValues } = props;
alert(JSON.stringify(values, null, 2));
try {
// compare initialValues and values
setLoggingFacilities(values.facilities);
const valuesToSend = values.facilities
.filter((facility, i) => {
// Let's send only those that have just been disabled or enabled:
return (
facility &&
initialValues[i] &&
facility.enabled !== initialValues[i].enabled
);
})
.map(facility => ({
key: facility.facilityName,
value: facility.enabled
}));
} catch (error) {
console.log("error catch", error);
}`
Haven't had much feedback asking this question in various other places.
I have a [codesanbox here](https://codesandbox.io/s/logging-table-l2rut)
Contributor guide
Research direction
Start with the enableReinitialize and onSubmit behavior described in the issue, then reproduce the case in the linked CodeSandbox. Determine whether the reported undefined initialValues is expected or a bug, and define done as a documented or tested way to compare the reinitialized values with submitted values.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- react, typescript
- Domain
- frontend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100