Skip to main content

Command Palette

Search for a command to run...

Responding to events and updating the screen in reactjs

Reactjs series from beginner to advanced levels ep6

Updated
2 min read
Responding to events and updating the screen in reactjs
P

Hello there! I am a full-stack software developer with a passion for front-end development. With years of experience under my belt, I bring a unique combination of technical skills and creative vision to every project I work on. I am dedicated to delivering visually appealing and intuitive interfaces that provide an exceptional user experience. Whether it's building responsive web applications or crafting complex full-stack applications, I am committed to delivering high-quality, scalable, and maintainable code. I am always seeking new challenges and opportunities to grow and stay on the cutting edge of the latest industry trends and technologies. I am excited to share my insights and expertise with you on this platform. Let's learn and grow together!

Responding to events

In ReactJS, you can respond to events by defining event handlers, which are functions that are called in response to specific events. You can attach event handlers to elements by using the on[event] attribute in JSX, where [event] is the name of the event you want to handle. For example, you can attach a click event handler to a button like this:

import React from "react";
import "./mybutton.css";

function MyButton() {
  //handle click function
  function handleClick() {
    alert("You clicked me!");
  }
  return (
      <div className="mybutton">
          {/* Any time the button is clicked a popup window appears with the message "you clicked me" */}
      <button
        onClick={handleClick}
        className="main-btn"
        style={{ margin: "3rem auto", padding: "0.5rem" }}
      >
        Click Here
      </button>{" "}
      {/* inline styles */}
    </div>
  );
}

export default MyButton;

Notice how onClick={handleClick} has no parentheses at the end! Do not call the event handler function: you only need to pass it down. React will call your event handler when the user clicks the button.

Updating the screen

Often, you’ll want your component to “remember” some information and display it. For example, maybe you want to count the number of times a button is clicked. To do this, add state to your component.

First, import useState from React:

import { useState } from 'react';

Now you can declare a state variable inside your component:

function MyButton() {  const [count, setCount] = useState(0);

You will get two things from useState: the current state (count), and the function that lets you update it (setCount). You can give them any names, but the convention is to call them like [something, setSomething].

The first time the button is displayed, count will be 0 because you passed 0 to useState(). When you want to change state, call setCount() and pass the new value to it. Clicking this button will increment the counter:

import React, { useState } from "react";
function MyButton() {  
const [count, setCount] = useState(0);  //setting the state count to 0
function handleClick() {    
setCount(count + 1);  }  
return (    
<button onClick={handleClick}>  Clicked {count} times    </button> // passing state to the handleClick function and calling it 
);}

React will call your component function again. This time, count will be 1. Then it will be 2. And so on.

ReactJS

Part 9 of 14

In this series I will be sharing reactjs related content from beginner to advanced levels.

Up next

Displaying data and conditional rendering in reactjs

Reactjs series from beginner to advanced levels ep5

More from this blog

E

Expert software Developer with Proven Track Record in Design and Development of High-Quality, User-Friendly Web Apps

34 posts

As a software developer, I am passionate about technology and have a drive to share my knowledge with others. With a focus on new and upcoming software developers, I aim to spread my expertise and experience to support their growth in the industry. Through my experience and dedication, I am constantly striving to expand my understanding of the latest trends and advancements in software development. Whether through writing articles or conducting presentations, I am dedicated to sharing my knowledge and helping others reach their full potential in the field.