-
-
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
Rename useFormik (and maybe useFormikContext) #1722
Comments
useFormik
and useFormikContext
useFormik
(and maybe useFormikContext
)
useFormik
(and maybe useFormikContext
)
Yep |
I came here to say this. Big thumbs up 👍 👍 👍 |
Already done. |
@jaredpalmer where I can find this changes? I use latest version of formik and still have possibility to import |
Instructions unclear :( I don't understand how to combine https://formik.org/docs/api/useFormik says: "Formik uses useFormik to create the component (which renders a React Context Provider). If you are trying to access Formik state via context, use useFormikContext." But where / how do I use const MyForm = () => {
// useFormikContext() ?
const formik = useFormik({
initialValues: { name: "" },
onSubmit: (values) => { console.log(values); }
});
return (
<form onSubmit={formik.handleSubmit}>
<MyField />
<button type="submit">Submit</button>
</form>
);
} const MyField = () => {
// useFormikContext() ?
const [field, meta, helpers] = useField("name");
return (
<input {...field} />
);
} |
@benneq the instructions say: By Formik, the instructions mean the // Formik
const Formik = ({ children, ...formikProps }) => {
// uses useFormik
const formik = useFormik(formikProps);
// to create the component which renders a React Context Provider
return <FormikProvider value={formik}>{props.children}</FormikProvider>;
} What this means is either, use Hooks can be hard to explain. Hope it helps! |
@johnrom Thank you! I do understand how hooks work. I was just kinda confused because of the sentence from the docs. I read "Formik" as "the library" and not as "the component". Maybe the docs should use Of course I could have looked at the source code of the components and hooks, but that would have made the docs pointless 😼 I now use const FormSubmitButton = (props) => {
const formik = useFormikContext();
return (
<button type="submit" disabled={formik.isSubmitting} {...props} />
);
}; 👍 |
I agree we can change the documentation to read:
Feel free to open a quick PR. They can be links to the documentation for the specific components / hooks, though maybe that would be too many links. |
🚀 Feature request
Current Behavior
Currently,
useFormik
is an internal hook used to initialize Formik, anduseFormikContext
is the hook used to actually use Formik functionality in user code. The problem is, when a user wants to "use Formik", they reasonably think, "duh, there's auseFormik
hook! how simple!"What I'm hearing around the issues here, is that the intended solution is simply to not export
useFormik
, and nobody will need to worry about it. However, I thinkuseFormik
can be useful if someone wants to take the core formik functionality and use it in a different context. I propose instead renaming these hooks to be more semantic and developer-friendly.Desired Behavior
We want a developer who is about to try and "use formik" to be able to see the
useFormik
hook and know they can depend on it, or at least not see auseFormik
due to the internal hook being semantically named.Suggested Solution
Rename the following hooks. These are suggestions which may not ultimately be what we settle on, but I think they move in the right direction.
useFormik
:useFormikInitializer
,useFormikCore
,useFormikInternals
, etcuseFormikContext
:useFormik
Alias:
useFormikContext
->useFormik
in case users have already started using this. Add a development time deprecation warning.Alternate Suggestion
Let there not be a
useFormik
hook at all, usinguseFormikInternals
(or whatever) anduseFormikContext
.Who does this impact? Who is this for?
Developers who want to "use Formik".
Describe alternatives you've considered
Answering dozens of questions about why
useFormik
doesn't work in Github Issues, Discord.Additional context
I think this will solve a lot of headaches from misunderstandings in the community, while allowing the flexibility to initialize Formik and use it in a different React.Context.
Examples of the misconception in action
#1719 (comment)
#1717
#1667
#1666
#1582
#1571
#1548
#1063 (comment)
The text was updated successfully, but these errors were encountered: