Mahamudul Hasan Rubel
HomeAboutProjectsSkillsExperienceBlogCoursesPhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • About
  • Projects
  • Skills
  • Experience
  • Blog
  • Courses
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 16 of the React Fundamentals: Build Modern UIs from Scratch course
ReactJune 25, 20264 min read

Handling Click Events: Interactive Components in React

Master React's onClick handler to make your app interactive. Learn how to trigger functions on user actions and pass event logic down to child components.

ReactJavaScriptWeb DevelopmentFrontendUIComponents

Previously in this course, we covered managing state with useState. Now that your components can hold data, it's time to make them respond to the user.

In this lesson, we’ll move beyond static rendering and learn how to capture user interactions. By the end, you’ll be able to trigger functions when a user clicks a button, a fundamental skill for building the interactive movie-browser app we've been working on.

Understanding onClick and Event Listeners

In standard HTML, you might use onclick="myFunction()" directly in your tags. React does things slightly differently. Because React uses a declarative approach, we pass a function reference to the onClick attribute (note the camelCase) on a JSX element.

When you attach an onClick listener to an element, React creates a SyntheticEvent—a cross-browser wrapper around the browser's native event. This ensures your code behaves consistently, whether your user is on Chrome, Firefox, or Safari.

A Basic Worked Example

Let’s add a "Like" button to our MovieCard component. When clicked, we want to log the movie's title to the console to confirm the interaction is working.

JSX
function MovieCard({ title }) {
  const handleLike = () => {
    console.log(CE9178">`You liked: ${title}`);
  };

  return (
    <div className="movie-card">
      <h3>{title}</h3>
      <button onClick={handleLike}>Like Movie</button>
    </div>
  );
}

In this snippet, we define handleLike inside the component. We then pass that function reference to the onClick prop of the button. Notice we pass handleLike (the reference), not handleLike() (the result of the function). If you add parentheses, the function will execute immediately when the component renders, which is almost certainly not what you want.

Passing Events to Child Components

As your app grows, you’ll often define interaction logic in a parent component and pass that logic down to a child. This is a common pattern for keeping your state management centralized.

Let's say we have a MovieBrowser component that keeps track of the "liked" status. We want to pass a function down to the MovieCard that handles the click.

JSX
function MovieBrowser() {
  const handleLike = (movieTitle) => {
    alert(CE9178">`Added ${movieTitle} to favorites!`);
  };

  return (
    <div>
      <MovieCard title="Inception" onLike={handleLike} />
    </div>
  );
}

function MovieCard({ title, onLike }) {
  return (
    <div className="movie-card">
      <h3>{title}</h3>
      <button onClick={() => onLike(title)}>Like</button>
    </div>
  );
}

Here, we used an arrow function inside the onClick prop: () => onLike(title). This is a common pattern for passing arguments from a child component back up to a parent-provided function.

Hands-on Exercise

Open your movie-browser project and perform the following steps:

  1. Locate your MovieCard component.
  2. Add a button element below the movie description.
  3. Create a function handleDetailsClick that logs "View details for [Movie Title]" to the console.
  4. Attach this function to the button using onClick.
  5. Challenge: Pass a custom message from the parent component as a prop, and have the handleDetailsClick function display that message in an alert when the button is clicked.

Common Pitfalls

Event handling is straightforward, but beginners often trip over these three issues:

  • Calling the function immediately: As mentioned, avoid onClick={handleClick()}. This executes the function during the render phase. Always use onClick={handleClick}.
  • Forgetting to prevent default behavior: If you are using buttons inside a <form> element, clicking the button will trigger a page reload by default. You’ll need to accept the event object in your handler and call event.preventDefault().
  • Confusing synthetic events with native events: While React events are similar to native DOM events, they are not identical. If you ever need to access the underlying browser event, you can still access it via event.nativeEvent.

Recap

We’ve covered the basics of making your UI interactive:

  • Use onClick (camelCase) to attach listeners in JSX.
  • Pass function references, not function calls.
  • Use arrow functions to pass arguments to event handlers.
  • Keep your logic clean by passing event handlers down as props when necessary.

These concepts form the foundation of how React apps handle user input. Once you master this, you'll find that React Event Handling: Understanding SyntheticEvents and Delegation becomes much easier to reason about as you scale your components.

Up next: We will learn how to update state based on its previous value to ensure our "Like" counters remain accurate.

Previous lessonBuilding an Interactive Search BarNext lesson Updating State Based on Previous State
Back to Blog

Similar Posts

ReactReactJune 24, 20263 min read

Setting Up with Vite: A Professional React Development Environment

Learn how to initialize a professional React project using Vite. We’ll cover the project directory structure and how to launch your fast development environment.

Read more
ReactReactJune 25, 2026

Part of the course

React Fundamentals: Build Modern UIs from Scratch

beginner · Lesson 16 of 49

  1. 1

    Introduction to Component-Based Architecture

    4 min
  2. 2

    Understanding the Virtual DOM

    4 min
  3. 3

    Setting Up with Vite

    3 min
4 min read

Mastering useEffect Dependencies: Control Your React Lifecycle

Learn to master the useEffect dependency array to control exactly when your side effects run. Avoid infinite loops and optimize your React components today.

Read more
ReactReactJune 25, 20263 min read

Building a Movie Filter Toggle in React: A Beginner's Guide

Master the art of UI state management by adding a filter toggle to your movie app. Learn to control list rendering using boolean state and checkbox inputs.

Read more
4

Mastering JSX Syntax

4 min
  • 5

    Creating Static Components

    3 min
  • 6

    Styling Components

    3 min
  • 7

    Passing Data with Props

    3 min
  • 8

    Dynamic Movie Cards

    3 min
  • 9

    Component Composition

    3 min
  • 10

    Conditional Rendering

    3 min
  • 11

    Rendering Lists of Data

    4 min
  • 12

    The Key Prop Explained

    4 min
  • 13

    Introduction to React State

    4 min
  • 14

    Managing State with useState

    3 min
  • 15

    Building an Interactive Search Bar

    3 min
  • 16

    Handling Click Events

    4 min
  • 17

    Updating State Based on Previous State

    4 min
  • 18

    Filtering the Movie List

    3 min
  • 19

    Handling Form Submissions

    3 min
  • 20

    Controlled Components

    4 min
  • 21

    Event Bubbling and Propagation

    3 min
  • 22

    Building a Movie Filter Toggle

    3 min
  • 23

    Introduction to Side Effects

    4 min
  • 24

    useEffect Dependencies

    4 min
  • 25

    Fetching Data from an API

    3 min
  • 26

    Handling Loading States

    3 min
  • 27

    Managing Errors

    3 min
  • 28

    Cleanup Functions in useEffect

    3 min
  • 29

    Debouncing Search Input

    3 min
  • 30

    Refactoring for Clean Code

    3 min
  • 31

    Folder Structure Best Practices

    4 min
  • 32

    Extracting Custom Hooks

    3 min
  • 33

    Prop Drilling and Context API

    Coming soon
  • 34

    Polishing the UI

    Coming soon
  • 35

    Finalizing the Movie Browser

    Coming soon
  • 36

    Review of Component Lifecycle

    Coming soon
  • 37

    Review of State Management

    Coming soon
  • 38

    Building a Modal Component

    Coming soon
  • 39

    Introduction to PropTypes

    Coming soon
  • 40

    Performance Optimization Basics

    Coming soon
  • 41

    Handling Browser History

    Coming soon
  • 42

    Working with LocalStorage

    Coming soon
  • 43

    Building a Favorites List

    Coming soon
  • 44

    Handling Media in React

    Coming soon
  • 45

    Introduction to Testing

    Coming soon
  • 46

    Debugging React Apps

    Coming soon
  • 47

    Deployment Basics

    Coming soon
  • 48

    Using External Libraries

    Coming soon
  • 49

    Advanced

    Coming soon
  • View full course