[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:
- Created 4 years ago
- Reactions:16
- Comments:11 (10 by maintainers)
Top 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 >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
Add it to formik native
Seemed like it was out of date so I implemented the same thing in #2176, will also update the docs and tests accordingly.