score:1
Accepted answer
On class methods your field be like:
class MyTextInput extends React.Component {
handleChange = value => {
const { name, onChange } = this.props;
onChange(name, value.target.value);
};
handleBlur = () => {
const { name, onBlur } = this.props;
if (onBlur) {
onBlur(name, true);
}
};
render() {
const { label, touched, errors, id, name, value, ...attributes } = this.props;
const err = getIn(errors, name);
const touch = getIn(touched, name);
return (
<>
<label htmlFor={id || name}>{label}</label>
<Field
{...attributes}
name={name}
value={value || ''}
className="text-input"
onChange={this.handleChange}
onBlur={this.handleBlur}
/>
{touch && err ? <div className="error">{err}</div> : null}
</>
);
}
}
And formik
field:
const SignupForm = () => {
return (
<>
<h1>Subscribe!</h1>
<Formik
...
>
{({ setFieldValue, setFieldTouched, values, errors, touched }) => (
<Form>
<MyTextInput
label="First Name"
name="firstName"
errors={errors}
touched={touched}
value={values.firstName}
onChange={setFieldValue}
onBlur={setFieldTouched}
placeholder="Jane"
/>
<MyTextInput
label="Last Name"
name="lastName"
type="text"
placeholder="Doe"
/>
<MyTextInput
label="Email Address"
name="email"
type="email"
placeholder="jane@formik.com"
/>
<button type="submit">Submit</button>
</Form>
)}
</Formik>
</>
);
};
Source: stackoverflow.com
Related Query
- React - Migrate Formik Function components to class Components
- React Function Components with hooks vs Class Components
- Is there a non-hook alternative for the React Material-UI makeStyles() function that works for class Components
- Refactor class to function components in React
- How to add a CSS class to an element with React function components
- React Class to Function components
- React - Error when class components reference function components
- Migrate a React class component to a Function component (React-google-maps)
- How to hide a modal using a function in class components in React
- When using react cdn links for a site does it require the use of class components or can I use function components?
- Convert React class components to function components
- React - Converting Class to Function Components For Front-End
- How do we call recursive function on react js in class based components
- Can I put class base components inside function base components to use react hooks?
- A property is undefined - switching from class components to function hooks components in React
- React function for making multiple class components
- When to use ES6 class based React components vs. functional ES6 React components?
- Passing in class names to react components
- What will happen if I use setState() function in constructor of a Class in ReactJS or React Native?
- Call a static function into the class React ES6
- Call external Javascript function from react components
- React - adding class to children components
- Should we use useCallback in every function handler in React Functional Components
- Apply style "cursor: pointer" to all React components with onClick function
- Converting React function component to class component issue
- React memo components and re-render when passing function as props
- React hooks: How to write variables in functional components that in class components were initialized in the constructor
- React hook form v7 Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()
- React Formik bind the external button click with onSubmit function in <Formik>
- How to use redux-toolkit createSlice with React class components
More Query from same tag
- When use useEffect, I get a warning when passing less than variable dependency
- Adding variable to JSX string
- React js- bootstrap Value not showing on Tab change
- Read a File through FileReader Api and Convert it into json object
- Add a loader while an if else statement is being evaluated
- React Component State Being Reset to Default
- Cannot fetch resource from one localhost server to another
- Can I call separate function in useEffect?
- react native animating multiple views as a circle
- Next.js - Client Side Navigation vs. changes in html
- React -Redux: Why is data not ready even if fetching is finished?
- Type for object in React/Typescript
- How to refactor this one function?
- Next.JS: Warning Expected server HTML to contain a matching <a> in <div>; How to rehydrate?
- Can't get the data from array react and Firestore
- React State Management: Context API as global store
- Force one selection checkboxe with REACT
- How can i access the navigation prop of the "DrawerCustom.Navigator" component, from another component
- React Native on Ipad - Error - could not connect to development server
- How to pass methods to form provider with different name to handle nested forms
- Handling events of forms
- How to access redux store from action creator?
- How to align a link in the middle of a paragraph?
- React Hooks - having trouble with splice updating state during onClick event
- How to have consistent width in material ui
- How to concatenate JSX components in React Native
- clicking label doesn't click checkbox in React?
- Local storage and persistent state in React
- reactjs function is calling after assigning variables
- How to "mock" navigator.geolocation in a React Jest Test