Handling forms in React involves using controlled components, which means that form elements like input fields, checkboxes, and radio buttons are controlled by React state. Here's a simple example of a controlled form in React:

In this example:

  • The useState hook is used to create a state variable formData that represents the data of the form.
  • The handleInputChange function is called whenever an input field value changes. It updates the corresponding property in the formData state.
  • The form has an onSubmit event handler that calls the handleSubmit function. This function prevents the default form submission behavior and can be used to perform any actions, such as submitting data to a server.

To use this component, you can simply include it in your main application file: