"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Props and Callbacks in a shell

Props and Callbacks in a shell

Published on 2024-11-06
Browse:203

Props and Callbacks in a shell

In this blog post, I will walk you through a practical scenario where a parent component (ListBox) interacts with a child component (AlertComponent) using props and callbacks.

This is useful in React when you want a child component to communicate back to the parent to maintain state or trigger actions.

Let's understand with the help of this example:

  • I have a ListBox component that displays a list of items. When the user performs a long press on any item, an alert dialog appears, asking whether the user wants to delete the item or not.

Here’s the interaction breakdown:

  1. ListBox (Parent) renders the items and passes necessary props and callbacks to the AlertComponent (Child).
import React, { useState } from 'react';
import AlertComponent from './AlertComponent';

const ListBox = () => {
  const [showComponent, setShowComponent] = useState(false);

  const alertAction = async () => {
    setShowComponent(!showComponent);
  };

  return (
    

Item 1

{/* Other list items */}
{/* Passing props to the child component */} { alert('Item Deleted'); setShowComponent(false); }} onCancel={() => setShowComponent(false)} showComponent={alertAction} />
); }; export default ListBox;
  1. The AlertComponent accepts props like title, description, and callbacks such as onAccept, onCancel, and a state-changing prop showComponent.
export const AlertComponent: = ({ title, description, 
onAccept, onCancel, showComponent }) => {
return (
... rest of the code
)
}
  1. The parent component needs to manage the visibility of the dialog, and the child component interacts with the parent by emitting events through callbacks to toggle this visibility.

showComponent is working as a callback as it's maintaining the state which is responsible to show/hide the AlertComponent

Whenever Reject is pressed, this callback will toggle the current state of showComponent.

 {
          alert('Item Deleted');
          setShowComponent(false);
        }}
        onCancel={() => setShowComponent(false)}
        showComponent={alertAction}
      />

Using props and callbacks in this way allows a clear flow of data between the parent and child components in React.

The parent can control state and pass it down to the child, while the child can communicate back via callbacks to inform the parent of any changes or actions the user has performed.

This is particularly useful for scenarios like showing alerts, modals, or pop-ups in response to user interaction.

Keep building!

Release Statement This article is reproduced at: https://dev.to/amanbhoria/props-and-callbacks-in-a-shell-4jk?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3