formik icon indicating copy to clipboard operation
formik copied to clipboard

Use React.forwardRef in <Formik>

Open jaredpalmer opened this issue 5 years ago • 13 comments

As of 2.1.2, <Formik> now allows for an innerRef prop. However, we should instead use React.forwardRef + React.useImperativeHandle. This is more intuitive and closer to best practices. The problem though, is that <Formik> takes TypeScript generics for Values and I'm not sure how to express this in with forwardRef. AFAICT, you can't because forwardRef returns a constant and constants can't have generics.

Help?!?!

jaredpalmer avatar Jan 14 '20 18:01 jaredpalmer

How about:

export const Formik = React.forwardRef(function Formik<
  Values extends FormikValues = FormikValues,
  ExtraProps = {}
>(props: FormikConfig<Values> & ExtraProps, ref: any) {
  const formikbag = useFormik<Values>(props);
  const { component, children, render, innerRef } = props;

  // This allows folks to pass a ref to <Formik />
  React.useImperativeHandle(innerRef, () => formikbag);
  React.useImperativeHandle(ref, () => formikbag);
  ...

This seems to infer okay, but I might be misunderstanding.

drivasperez avatar Jan 14 '20 19:01 drivasperez

Submit a PR?

jaredpalmer avatar Jan 14 '20 19:01 jaredpalmer

Done!

drivasperez avatar Jan 14 '20 20:01 drivasperez

Ah, never mind, I see the issue.

drivasperez avatar Jan 14 '20 20:01 drivasperez

Doing a little cheating makes this possible. I didn't test too thoroughly, but it seems to work. The main issue is that I had to override the type inference (with as) and so this particular assignment will now be vulnerable to incorrect typings in the future.

https://github.com/jaredpalmer/formik/pull/2222

Some external issues related to forwardRef woes: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/35834 https://stackoverflow.com/a/51898192

And finally, the rabbit hole, where someone attempts to define a way to let TS infer an open generic, and the TS team says "maybe one day": https://github.com/microsoft/TypeScript/pull/24626

johnrom avatar Jan 17 '20 18:01 johnrom

I couldn’t find anything that worked before that doesn’t now. I guess the main concern is: going forward, how can you break it going forward in a way that the “as [...component...]” cast won’t complain about, and is that likely to happen?

drivasperez avatar Jan 20 '20 22:01 drivasperez

Basically, a Formik contributor could change the types incorrectly. I wish we could ignore a specific rule so we wouldn't have to use as and we could protect against all the other errors, but for now as is the best protection we can get. as won't protect against most things, but it'll at least make sure it's kind of the same signature. I made a similar comment here: https://github.com/microsoft/TypeScript/pull/30215

johnrom avatar Jan 21 '20 01:01 johnrom

It touches https://github.com/jaredpalmer/formik/issues/2290

ProteanCode avatar Feb 14 '20 13:02 ProteanCode

A related StackOverflow I thought would be helpful to consider: https://stackoverflow.com/questions/58469229/react-with-typescript-generics-while-using-react-forwardref (answer with some approaches)

tony avatar May 04 '20 14:05 tony

Any updates on this issue? It's preventing my team from upgrading to Formik 2.x

stclairdaniel avatar Jul 29 '20 16:07 stclairdaniel

+1

julioxavierr avatar Nov 12 '20 18:11 julioxavierr

What about:

const MyComponent = React.forwardRef(function<T>(props: PropsType<T>, ref) {
  return null;
})

arvigeus avatar Jan 17 '21 11:01 arvigeus

any update on this issue ? we are facing same issue while upgrading formik ?

workforbala avatar Mar 24 '22 14:03 workforbala

To access formik by reference, you can do like this:

const formRef = useRef<FormikProps<FormProps>>(null);

<Formik innerRef={formRef} />

  • FormProps iis the model of the data that you will use in the form

image

image

If you print the reference console.log(formRef.current) you will see something like this:

image

This way it is possible to manipulate formik from anywhere :smile: :sunglasses: that's it

Evaldoes avatar May 12 '23 08:05 Evaldoes