Disabling the Submit Button

Learn how to dynamically disable the "Submit" button.

Often, we don’t want our users to click the “Submit” button until they complete an action or until a condition is fulfilled. One way to make sure this happens is to display a message to the user that tells them not to click the button until they complete the action. This isn’t very dependable because it takes control away from the form.

We have to disable the button ourselves so that when the user clicks it, nothing happens. To disable a button in JavaScript XML (JSX), we do this:

<button disabled={true}>...</button>

But what we want is a way for us to tell our “Submit” button when it should be disabled and when it should not.

We’ll look at two instances in which we may want to disable our “Submit” button:

  • When at least one field in the form is invalid.
  • When the form is processing submission logic that will take a while.

Get hands-on with 1200+ tech skills courses.