-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Use React.forwardRef in <Formik> #2208
Comments
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 Some external issues related to 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": microsoft/TypeScript#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 |
It touches #2290 |
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:
If you print the reference This way it is possible to manipulate formik from anywhere 😄 😎 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?!?!
The text was updated successfully, but these errors were encountered: