Passing State Back to a Parent in React
Tags: jsx, react, beginners, programming
Published: 2023-09-03 16:31:56

In React, passing state from a child component back to its parent can be achieved using callback functions. This powerful technique allows for seamless communication between components. To understand this concept better, let's explore a metaphor: giving a child a cellphone. Just as a cellphone allows a child to communicate with their parent, a callback function enables a child component to send data to its parent component. In this blog, we will delve into the concept of callback functions and provide code examples to illustrate their usage.

A callback function is a function passed as a prop from a parent component to a child component. This callback function allows the child component to send data or trigger an action in the parent component. It establishes a communication channel between the child and the parent, similar to how a cellphone enables communication between a child and their parent.

Imagine a scenario where a child wants to inform their parent about something important after the child has left the house. The parent gives the child a cellphone and instructs them to call whenever they need to share information. Similarly, in React, the parent component passes a callback function to the child component, allowing the child to communicate with the parent whenever necessary.

Let's consider a simple React application where a parent component, Parent, renders a child component, Child. The child component has a button, and when clicked, it triggers the callback function passed from the parent, sending a message back to the parent.

```
// Parent.js

import React from 'react';
import Child from './Child';

const Parent = () => {
  const handleCallback = (message) => {
    console.log(`Received message from child: ${message}`);
  };

  return (
    <div>
      <h1>Parent Component</h1>
      <Child cellPhone={handleCallback} />
    </div>
  );
};
```

export default Parent;

```
// Child.js

import React from 'react';

const Child = ({ cellPhone }) => {
  const callParent = () => {
    cellPhone("Hello from Child! I'm out in the world, but i can still send you information! thanks for this callback function!");
  };

  return (
    <div>
      <h2>Child Component</h2>
      <button onClick={callParent}>Send Message</button>
    </div>
  );
};
```

export default Child;
In this example, the parent component defines the handleCallback function, which logs the received message to the console. The callback prop is passed to the child component, where it is triggered when the button is clicked. The child component calls the callback function, passing the message "Hello from Child! I'm out in the world, but i can still send you information! thanks for this callback function!" back to the parent.

Using callback functions in React allows for efficient communication between parent and child components. Just as giving a child a cellphone enables them to share information with their parent, passing a callback function from a parent component to a child component establishes a channel for data transfer. By understanding this metaphor and implementing callback functions, you can enhance the interactivity and functionality of your React applications.