0

From https://react-hook-form.com/api/useform/formstate I am using the isValid state to check if input matches the rules

 <form> <Controller name="username" control={control} rules={{ minLength: { value: 2, message: 'Tx', }, pattern: { value: /^([A-Za-z0-9_](?:(?:[A-Za-z0-9_]|(?:\.(?!\.))){0,28}(?:[A-Za-z0-9_]))?)/, message: 'x', }, }} render={({ field: { ref, ...field } }) => ( <SecondaryInput name="username" errors={errors} innerRef={ref} {...field} /> )} /> <PrimaryButton disabled={!isDirty || !isValid} type="submit" > Test Submit </PrimaryButton> </form> 

And it works:

  1. Button is disabled (empty input)
  2. I type
  3. Button not disabled anymore

But when I delete everything (empty input) the button is not disabled. The isValid state remains true

It is probably due to the isDirty state but I don't know how the code works without the isDirty state

2
  • Did you use the handleSubmit of react-hook-from in form tag? Commented Aug 16, 2022 at 16:50
  • Yes, just to log the data in the console. Why? Commented Aug 16, 2022 at 16:58

1 Answer 1

1

The information that you gave is quite not enough. Make sure that you are using a function to handle changes to your input field using the onChange property. Use the useState function to declare and store the value of the user input and the boolean values isDirty and isValid.

On your function that handles the change to the input field, make sure to check whether the input is valid/dirty or not and update the boolean variables as needed.

Since you declared your variables with useState, react updates the state and your web page should behave the way you want it to.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.