Open jaredpalmer opened 4 years ago
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.
Submit a PR?
Done!
Ah, never mind, I see the issue.
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
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?
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
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)
Any updates on this issue? It's preventing my team from upgrading to Formik 2.x
+1
What about:
const MyComponent = React.forwardRef(function<T>(props: PropsType<T>, ref) {
return null;
})
any update on this issue ? we are facing same issue while upgrading formik ?
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 formIf you print the reference console.log(formRef.current)
you will see something like this:
This way it is possible to manipulate formik from anywhere :smile: :sunglasses: that's it
As of 2.1.2,
<Formik>
now allows for aninnerRef
prop. However, we should instead useReact.forwardRef
+React.useImperativeHandle
. This is more intuitive and closer to best practices. The problem though, is that<Formik>
takes TypeScript generics forValues
and I'm not sure how to express this in withforwardRef
. AFAICT, you can't because forwardRef returns a constant and constants can't have generics.Help?!?!