DEV Community

Erasmus Kotoka
Erasmus Kotoka

Posted on

1

Handling Events and Forms in React.js

Understanding how to handle events and forms in React.js is crucial for creating interactive and user-friendly web applications.

In React, handling events is quite similar to handling events on DOM elements. However, there are some syntactic differences:

  1. Event Handling:
    • In React, events are named using camelCase, rather than lowercase.
    • You pass a function as the event handler, rather than a string.

For example:

   <button onClick={this.handleClick}>Click Me</button>
Enter fullscreen mode Exit fullscreen mode
  1. Forms and Controlled Components:
    • Forms in React work a bit differently. By default, form elements maintain their own state and update it based on user input.

In React, mutable state is typically kept in the state property of components and only updated with setState().

  • Controlled components are those where React controls the value of form elements.

The form data is handled by the React component rather than the DOM itself.

Example of a controlled component:

   class NameForm extends React.Component {
     constructor(props) {
       super(props);
       this.state = { value: '' };

       this.handleChange = this.handleChange.bind(this);
       this.handleSubmit = this.handleSubmit.bind(this);
     }

     handleChange(event) {
       this.setState({ value: event.target.value });
     }

     handleSubmit(event) {
       alert('A name was submitted: ' + this.state.value);
       event.preventDefault();
     }

     render() {
       return (
         <form onSubmit={this.handleSubmit}>
           <label>
             Name:
             <input type="text" value={this.state.value} onChange={this.handleChange} />
           </label>
           <input type="submit" value="Submit" />
         </form>
       );
     }
   }
Enter fullscreen mode Exit fullscreen mode

In this example, the value of the input is bound to the component's state, and the handleChange method updates the state with the user's input.

When the form is submitted, the handleSubmit method alerts the current value of the input.

COdeWith

KOToka

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay