[v2] better error message when useField is used above Formik (TypeError: formik.getFieldProps is not a function)
See original GitHub issue🐛 Bug report
Current Behavior
using useField
above a Formik
instance generates this error:
Uncaught TypeError: formik.getFieldProps is not a function
at useField (formik.esm.js:993)
at NewReview (NewReview.tsx:41)
Expected behavior
a nicer error message
Reproducible example
import React from "react";
import { render } from "react-dom";
import { Formik, useField } from "formik";
import { DisplayFormikState } from "./helper";
import "./style.css";
const App = () => {
const [field, meta] = useField("name"); // this is wrong usage
return (
<div className="app">
<Formik
initialValues={{ name: "" }}
onSubmit={() => {}}
>
{props => {
/**
* Add your code here
*/
return (
<form onSubmit={props.handleSubmit}>
<h1>Your code here</h1>
<button type="submit">Submit</button>
<DisplayFormikState {...props} />
</form>
);
}}
</Formik>
</div>
);
};
render(<App />, document.getElementById("root"));
https://codesandbox.io/s/formik-codesandbox-template-4yqz7
Suggested solution(s)
lets have a nicer error? if formik.getFieldProps doesnt exist, warn the user that they’re using formik wrong
Your environment
Software | Version(s) |
---|---|
Formik | 2.x |
React | 16.8 |
TypeScript | 3 |
Browser | chrome |
npm/Yarn | 6 |
Operating System | mac |
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:6 (2 by maintainers)
Top Results From Across the Web
formik.getFieldProps is not a function - Stack Overflow
this error is raised because you are using getFieldProps() function on Field component provided by formik and not on normal component like ...
Read more >formik.getFieldProps is not a function - Bountysource
[v2] better error message when useField is used above Formik (TypeError: formik.getFieldProps is not a function)
Read more >useField() - Formik
useField is a custom React hook that will automagically help you hook up inputs to Formik. You can and should use it to...
Read more >Testing Formik with react-testing-library - Scott Sauber
tldr; Having issues testing Formik with react-testing-library? Validation errors not showing up? Formik validation happens asynchronously, ...
Read more >Formik + getFieldProps - CodeSandbox
Activating extension 'vscode.typescript-language-features' failed: Could not find bundled tsserver.js.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hello !
I dug a bit into this, it fails because it was used outside of a Formik context. There is a protection for it that should have triggered (see
invariant
in ./src/FormikContext.tsx). The thing is the default value of the context is set to{}
on line 5. Meaning that if you don’t set a context, there will still be a non-null context, thus, the warning will not trigger.An easy fix would be to replace the default value with
undefined
, I think. Thanks for signaling this bug.Is there a solution to this?