Formik field loses focus when used with error props

Viewed 1188

When I type something in my custom field it loses focus and needs to be clicked again in order to add data, Below is the sandbox link to code. It only happens when I pass error and touched props to check for validation, If I remove that it works perfectly.

https://codesandbox.io/s/formik-example-forked-w0bub?file=/index.js

Browser I'm using is Chrom for windows

1 Answers

The input inside the functional component is recreated every time when there is state changes.

Read this blog post to know more the behaviour of nested functional components

To avoid recreation problem, you can follow the Formik example to create other functional component and use it inside your component.

const MyInput = ({ field, form, ...props }) => {
  return <input {...field} {...form} {...props} />;
};

...

<Field
  id="email"
  name="email"
  placeholder="Email"
  component={MyInput}
/>

Here is the codesandbox for demo.

Related