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] useField: bound name to onChange and onBlur

See original GitHub issue

🚀 Feature request

Current Behavior

I’m using useField on react-native and since TextInput has no name attribute, I need to bind name to handlers manually:

const TextField: React.SFC<TextFieldProps> = ({ name, ...props }) => {
  const [field] = useField(name);

  return (
    <TextInput
      {...props}
      value={field.value}
      onChangeText={field.onChange(name)}
      onBlur={field.onBlur(name)}
    />
  );
};

Desired Behavior

I spect to use onChange and onBlur handlers like this:

const TextField: React.SFC<TextFieldProps> = ({ name, ...props }) => {
  const [field] = useField(name);

  return (
    <TextInput
      {...props}
      value={field.value}
      onChangeText={field.onChange}
      onBlur={field.onBlur}
    />
  );
};

Suggested Solution

Bind name in formik.getFieldProps. I think here: https://github.com/jaredpalmer/formik/blob/377c36e3d1fecc0f0556d0218a18725ad04a7505/src/Formik.tsx#L765-L770 It should be replaced with:

      const field: FieldInputProps<any> = {
        name,
        value: valueState,
        onChange: handleChange(name),
        onBlur: handleBlur(name),
};

Can this break useField for web users?

Possible workarounds

Create a useFieldNative:

import * as React from 'react';

import { useField, FieldMetaProps } from 'formik';

interface FieldInputNativeProps {
  value: string;
  onChangeText: (text: string) => void;
  onBlur: () => void;
}

export default function useFieldNative(
  name: string,
): [FieldInputNativeProps, FieldMetaProps<any>] {
  const [field, meta] = useField(name);
  const onChangeText = React.useCallback(field.onChange(name), [
    name,
    field.onChange,
  ]);
  const onBlur = React.useCallback(() => field.onBlur(name), [field, name]);

  const fieldNative: FieldInputNativeProps = {
    value: field.value,
    onChangeText,
    onBlur,
  };

  return [fieldNative, meta];
}


const TextField: React.SFC<TextFieldProps> = ({ name, ...props }) => {
  const [field] = useFieldNative(name);

  return <TextInput {...props} {...field} />;
};

Who does this impact? Who is this for?

react-native users

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:16
  • Comments:11 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
jaredpalmercommented, Jan 7, 2020

Add it to formik native

0reactions
zingerjcommented, Jan 7, 2020

Seemed like it was out of date so I implemented the same thing in #2176, will also update the docs and tests accordingly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

useField - VeeValidate
useField is a custom composition API function that allows you to create data ... setup> import { useField } from 'vee-validate'; // a...
Read more >
how to call 'onchange' and 'onblur' js event name in same input?
Please see below code. <html> <bods > <input type="text" value="here is a text field" onblur="alert('Blur Event!')" onchange="alert('Change ...
Read more >
withFormik() | Formik
14 <form onSubmit={handleSubmit}>. 15 <input. 16 type="text". 17 onChange={handleChange}. 18 onBlur={handleBlur}. 19 value={values.name}. 20 name="name".
Read more >
Input Components - React-admin - Marmelab
Then you can display a text input to edit the author's first name as ... useInput will call the provided onChange and onBlur...
Read more >
Managing list of form fields with formik through example
Custom hooks are now part of formik >= v2 , useField hook returns a ... It accepts either a string of a field...
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