-
-
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
Expose formikbag as imperative methods #1972
Expose formikbag as imperative methods #1972
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit dee856e:
|
I think this should work? |
@johnrom thoughts? |
I'm very much in dotnet-land right now, but if you want feedback on the TypeScript I think you know my answer :). There should be a proper type (typeof formikBag ?) instead of export type FormikRef<Values> = FormikBag<Values>; // change as necessary
export interface FormikConfig<Values> extends FormikSharedConfig {
innerRef: React.MutableRefObject<FormikRef<Values>>;
} Additionally, I don't know that a reference to the FormikBag is equivalent to a "reference" to the Formik component, and I think there are better ways to access the Formik API than prop drilling -- there is discussion around this on the issue linked above. Here's an example using Context. It's verbose, but with the benefit of being completely customizable. type FormikSubmitContext = {
registerOnSubmit: (handler: FormikHandlers['onSubmit']) => void;
onSubmit?: FormikHandlers['onSubmit'];
}
const formikSubmitContext = React.createContext<FormikSubmitContext | undefined>();
const MyParentComponent = () => {
const [onSubmit, registerOnSubmit] = React.useState<FormikHandlers['onSubmit']>();
return <formikSubmitContext.Provider value={{
onSubmit,
registerOnSubmit
}}>
{/* imagine these using actual fn components */}
<Formik>
{formikProps => {
const myContext = React.useContext(formikSubmitContext);
if (!myContext) throw new Error("no context");
React.useEffect(() => {
myContext.registerOnSubmit(formikProps.onSubmit);
return () => myContext.registerOnSubmit(undefined);
}, [formikProps.onSubmit, myContext.registerOnSubmit])
}}
</Formik>
{() => {
const myContext = React.useContext(formikSubmitContext);
return <button onClick={myContext.onSubmit}>Submit</button>
}}
</formikSubmitContext.Provider>
} |
@johnrom thanks for the review. |
Any updates on this? |
Why can't we just do useImperativeHandle? |
I'm ok with this |
@jaredpalmer Is the path forward here to do |
@ksweetie yes |
And update the PR to deal with Lerna |
@ksweetie want to take a stab at this? |
Is there any progress on this? It would make my life easier and I don't mind taking it on, but I don't want to swoop in if it's already being worked on. |
I'll fix the conflicting files |
This pull request is being automatically deployed with ZEIT Now (learn more). 🔍 Inspect: https://zeit.co/jared/formik-docs/h6sn0i51f |
Using
if I do:
i still get:
and Is this not the right way to be using this now? It used to work in a past version of react and formik, just going through a round of upgrading all my deps... Edit: Nevermind, now you use
It also doesn't expose the same object that the old versions used to as |
Motivation
Since Formik 2 is built on top of React Hooks it was not possible to attach a ref and consequently, some people can't upgrade to v2. It resolves #1603.
My first approach was to use
React.forwardRef
like that:But it breaks the interface FormikConfig giving the following errors:
I've managed to make it work when I changed
FormikProps<Values>
toFormikProps<any>
onrender?
,onReset
,validate
, but I don't know how to properly fix these types 😞Do you guys have any thoughts on this? I would appreciate any feedback 😬