Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to add onclick event in ReactJS on child component

What's an OnClick Event?

Before we dive into how to add an onClick event in ReactJS on a child component, let's first shed some light on what an onClick event is. In simple terms, onClick is what happens when you click something on a webpage. For example, when you click a button and a pop-up appears, that's an onClick event.

Why Do We Need OnClick Events?

Consider onClick events as a way of interacting with the user. They allow us to make our websites more interactive and responsive. Imagine a website with no buttons to click, no forms to fill, no images to zoom in on. Pretty dull, right? onClick events are the unsung heroes that bring a website to life.

Adding an OnClick Event in ReactJS

ReactJS, a JavaScript library, makes it easier to build interactive user interfaces. Adding an onClick event in ReactJS is not too different from how it's done in plain JavaScript, but with some subtle differences.

In regular JavaScript, we often attach an onClick event directly to an HTML element like this:

<button onclick="myFunction()">Click me</button>

But in ReactJS, it's slightly different. Here, onClick events are attached to components. Let's look at an example:

<button onClick={this.handleClick}>Click me</button>

Did you notice the difference? In ReactJS, onClick is camelCased and is enclosed in curly braces {}. Also, the function to be executed (in this case, this.handleClick) is passed without the parentheses. Using parentheses would call the function immediately after the component renders, which is not what we want.

Child Component OnClick Events

Now, let's get to the crux of our discussion: adding an onClick event on a child component. In ReactJS, components can have parent-child relationships. Think of it like a family tree. The parent component can have multiple child components, and these children can have their own children. This structure helps us organize our code better and makes it more maintainable.

Let's consider a simple example where we have a parent component called Parent and a child component called Child. Our goal is to trigger an onClick event on the Child component that will be handled by the Parent component.

Our Parent component would look something like this:

class Parent extends React.Component {
    handleClick() {
        console.log('Click happened');
    }
    render() {
        return <Child onClick={this.handleClick} />
    }
}

And our Child component:

class Child extends React.Component {
    render() {
        return <button onClick={this.props.onClick}>Click me</button>
    }
}

Here, the Parent component is passing the handleClick function to the Child component as a prop. The Child component then assigns this function to the onClick event of the button. Now, whenever the button is clicked, the handleClick function of the Parent component is executed.

Final Thoughts

And there you have it! That's how you add an onClick event to a child component in ReactJS. Trust me, it's not as complicated as it might sound at first. It's just like playing catch. The parent (component) throws the ball (function), and the child (component) catches it (via props) and throws it back (using the onClick event).

Remember, the power of ReactJS lies in its component-based architecture. Embrace it and you'll find yourself building complex UIs with ease. Happy coding!