Home > Web Front-end > JS Tutorial > Props and Callbacks in a shell

Props and Callbacks in a shell

Linda Hamilton
Release: 2024-10-02 06:28:30
Original
271 people have browsed it

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<boolean>(false);

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

  return (
    <div>
      <div onLongPress={alertAction}>
        <p>Item 1</p>
        {/* Other list items */}
      </div>

      {/* Passing props to the child component */}
      <AlertComponent
        title="Deleting item?"
        description="Click Accept to delete."
        onAccept={() => {
          alert('Item Deleted');
          setShowComponent(false);
        }}
        onCancel={() => setShowComponent(false)}
        showComponent={alertAction}

      />
    </div>
  );
};

export default ListBox;
Copy after login
  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 (<AlertDialog>
... rest of the code
</AlertDialog>)
}
Copy after login
  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.

<AlertComponent
        title="Deleting item?"
        description="Click Accept to delete."
        onAccept={() => {
          alert('Item Deleted');
          setShowComponent(false);
        }}
        onCancel={() => setShowComponent(false)}
        showComponent={alertAction}
      />
Copy after login

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!

The above is the detailed content of Props and Callbacks in a shell. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template