question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[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:open
  • Created 4 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
Granipousscommented, Jul 30, 2019

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.

0reactions
jzucadicommented, Feb 22, 2022

Is there a solution to this?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found