jaredpalmer / formik

Build forms in React, without the tears 😭
https://formik.org
Apache License 2.0
33.87k stars 2.78k forks source link

Custom enableReinitialize callback (or a different name?) #2428

Open johnrom opened 4 years ago

johnrom commented 4 years ago

🚀 Feature request

Current Behavior

enableReinitialize={true} only reinitializes in certain circumstances, only reinitializes certain things, and is not extensible.

Desired Behavior

Instead, I think we should allow a user to trigger any kind of re-renders they need.

Suggested Solution

<Formik 
    initialValues={newValues} 
    enableReinitialize={(newProps, oldProps) => ({
        ...newProps,
        // or touched, or isDirty, or whatever you want to override
        values: {
            ...oldProps.values,
            ...newProps.initialValues,
        },
    })} 
/>

Who does this impact? Who is this for?

People with advanced reinitialization needs.

Describe alternatives you've considered

Telling people to create custom effects.

Additional Context

181

maddhruv commented 3 years ago

Hey @johnrom are you working on this? or have some plans? I think we might also need to change the prop name or introduce a new prop for this. As enable keyword is a boolean keyword - either true or false.

I like the name that React has shouldComponentUpdate

johnrom commented 3 years ago

I don't have any plans to work on this one, it just came from a bunch of issues and I wanted to formalize a potential solution.

I think shouldX only really applies for boolean hooks, whereas this one should be more reducer-like, like:

// on reinitialize, set all fields to touched
const onReinitialize = (state: Readonly<FormikState<Values>>, props: FormikProps<Values>) => ({
  ...state,
  touched: setNestedObjectValues(
    props.initialValues,
    true
  );
});