From the course: Master React 19 and Next.js 16 with Hands-On Projects and Real-World Applications

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Passing event handlers from a parent to a child

Passing event handlers from a parent to a child

In this session, you'll learn how to pass event-handling logic from a parent component to a child component in React. This is a fundamental concept for building interactive and reusable React applications. Let's start with our initial app.js file. Let's create a Button.jsx file inside the src folder. Moving to Button.jsx. This file will house our reusable Button component. Currently, it's just an empty component, accepting onClick and children props. The onClick prop will hold our event handler function, and children will allow us to pass content inside the button, like text. You will notice I am destructuring props for brevity and readability. Let's add the button's functionality. This line renders a button element. The onClick attribute calls the function passed as the onClick prop whenever the button is clicked. And children renders any content passed between the button tags in the parent component. You can change the name of the OnClick prop to anything you like. When you create a…

Contents