Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react hook form with controlled input

export default function App() {
  const { control, handleSubmit } = useForm({
    defaultValues: {
      textField: "",
      checkbox: false
    }
  });

  const onSubmit = (values) => alert(JSON.stringify(values));

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <Controller
        control={control}
        name="textField"
        render={({ field }) => (
          // Material UI TextField already supports
          // `value` and `onChange`
          <TextField {...field} label="Text field" />
        )}
      />

      <Controller
        control={control}
        name="checkbox"
        render={({ field: { value, onChange } }) => (
          // Checkbox accepts its value as `checked`
          // so we need to connect the props here
          <FormControlLabel
            control={<Checkbox checked={value} onChange={onChange} />}
            label="I am a checkbox"
          />
        )}
      />

      <Button type="submit" variant="contained" color="primary">
        Submit
      </Button>
    </form>
  );
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: getusermedia close stream 
Javascript :: Using the Sanity client without specifying an API version is deprecated 
Javascript :: configure angular router apache 
Javascript :: Jquery check if hover over child element 
Javascript :: node js gitignore 
Javascript :: React Javascript Builtin Hooks Import bug 
Javascript :: alert message 
Javascript :: javascript object array sum of values in object 
Javascript :: how to pass a component as a prop in react 
Javascript :: get two types of date formate datepicker 
Javascript :: Highlight current nav link in react 
Javascript :: heroku proxy cross origin 
Javascript :: how to remove letters from an array javascript 
Javascript :: jquery clone table row 
Javascript :: check if an array is empty 
Javascript :: regular expression url 
Javascript :: react hooks link to external site 
Javascript :: Javascript basic arrow function 
Javascript :: delete value from json array with index 
Javascript :: elasticsearch response format 
Javascript :: calcular sobra de divisão de parcelas js 
Javascript :: vuejs nested v-for 
Javascript :: js addeventlistener keyup not working on phone 
Javascript :: react hook useeffect 
Javascript :: new date.gettime() is not a constructor 
Javascript :: express and node pakages 
Javascript :: make alphabet js 
Javascript :: compare two dates in javascript 
Javascript :: if text is present make div hide 
Javascript :: export csv single javascript 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =