Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

ReactJS How to call a parent function in child component

Understanding ReactJS Component Structure

In the world of ReactJS, components are the building blocks of your application. Imagine you are constructing a Lego model - each piece is a component. Some pieces are larger (parent components), while others are smaller (child components). The larger pieces can hold and connect the smaller ones. This is similar to how parent and child components interact in ReactJS.

What Is a Parent Function?

Now that we understand the basic concept of components, let's delve into functions. A function, in programming, is a set of instructions that performs a task. It's like a mini-program within your application.

In the context of parent and child components, a parent function is a function that is defined in the parent component. This function can be passed down to a child component, which can then execute it.

Think about it like this: a parent gives a remote control (the function) to a child. The child doesn't know how the remote control was built (the function details), but they can push its buttons (call the function) to make things happen (perform the function's task).

Passing a Parent Function to a Child Component

Okay, now that we have an idea of what a parent function is, let's look at how we can pass it to a child component in ReactJS.

Let's say we have a parent component called App and a child component called Button. In our App component, we have a function called handleClick that we want to pass to Button.

Here's how our App component might look like:

class App extends React.Component {
  handleClick() {
    alert('Button clicked!');
  }

  render() {
    return (
      <Button />
    );
  }
}

In the code above, we are defining a parent function handleClick which triggers an alert when called. The Button component is defined in the return statement of the render method, but we have not yet passed the handleClick function to it.

To pass the function to the child component, we need to include it as a prop:

class App extends React.Component {
  handleClick() {
    alert('Button clicked!');
  }

  render() {
    return (
      <Button onClick={this.handleClick} />
    );
  }
}

In the updated code, we've added the onClick prop to the Button component. This prop is assigned the handleClick function. Now, our Button component has access to this function.

Calling the Parent Function in the Child Component

Now, let's switch to our Button component. Here's a basic version of it:

class Button extends React.Component {
  render() {
    return (
      <button>Click Me!</button>
    );
  }
}

The Button component is just a simple button that doesn't do anything when clicked. We need to connect the handleClick function passed from the parent component to this button.

Here's how we can do that:

class Button extends React.Component {
  render() {
    return (
      <button onClick={this.props.onClick}>Click Me!</button>
    );
  }
}

We've added an onClick attribute to the button element, and we've set it to this.props.onClick. The props is like a bag that carries the values from the parent component to the child component. By calling this.props.onClick, we are saying "call the function that was passed as the onClick prop".

Now, when you click on the button, you should see the alert from the handleClick function!

Conclusion

In the Lego world of ReactJS, a child component can use a remote control given by its parent component. This remote control is a metaphor for a function. By passing this function from the parent to the child, the child can execute the function and trigger actions in the application.

This flexibility allows components to interact, making our ReactJS applications more dynamic and interactive. Just as you can build amazing structures with Lego by combining different pieces, you can create complex and powerful applications by combining ReactJS components and functions in creative ways.

Remember, programming is all about problem-solving and creativity. Keep exploring, keep learning, and most importantly, have fun!