What is the best way to establish communication between a child and grandfather component in React?

I am currently developing an application using Angular 2. In my specific situation, I have three main components:

  • SearchComponent: This component is responsible for calling a web service and injecting the results into SearchResultComponent.
  • SearchResultComponent: Here, the data is displayed in a table format.
  • EditComponent: This component allows for editing of the data.

Here is the code for SearchComponent:

<form>
    ... Input fields for the form.
</form>

<!-- The result property is within the SearchComponent class -->
<search-result [(value)]="result"></search-result>

Below is the code for SearchResultComponent:

<div>
    <table>
        ... Table content here
        ... Edit component for each row
        <tr>
            <td>data</td>
            <td>
                <edit></edit>
            </td>
        </tr>
    </table>
</div>

Next, we have the code for EditComponent:

<form>
    ... Edit inputs go here...
</form>

<button label="Save" (click)="edit()"></button>

<edit [(documentId)]="id"></edit>

The ultimate goal is to reload the results when the save button is clicked.

How can this goal be achieved?

Thank you!

Answer №1

There are multiple ways to accomplish this task.
1. Approach: Develop a singleton service as recommended by @AsifKarimBherani.

The singleton pattern is a design principle that limits the creation of a class instance to just one object, which can be beneficial when there is a need for a single object to manage actions across the system.

For simpler scenarios in Angular 2+, you can implement this by providing the service solely in the app.module.ts file (If following the standard structure from Angular-CLI).

2. Approach: Establish a communication bridge between the grandchild and parent components, and another bridge between the parent and grandparent components utilizing the @Output decorator.

The child component exposes an EventEmitter property, through which it sends out events when certain actions occur. The parent component listens to these events and responds accordingly.

The EventEmitter property in the child component acts as an output property, typically marked with the @Output decorator.

Hopefully, this has sparked some inspiration for your project.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to develop an Angular library with components in version 8 that can be seamlessly integrated into upcoming Angular versions such as 12, 13, and 14

Do I need to implement a new technique or setup in order to understand this? Can we use Angular elements as the only solution, or are there alternative approaches available? ...

Creating a Dynamic Download Link in Angular 6

Hello everyone, I need assistance in making my download feature dynamic. Here is my HTML code: <button class="btn btn-primary" (click)="downloadMyFile({{account.students[0].schoolId}})"><i class="fa fa-file-pdf-o"></i> Download</butt ...

Oh no, an issue has occurred with The Angular Compiler! It appears that TypeScript version 3.9.10 was found instead of the required version, which should be >=3.6.4 and <

After upgrading my angular application from version 5 to version 9, I encountered an issue while trying to deploy my code on the server. ERROR in The Angular Compiler requires TypeScript >=3.6.4 and <3.9.0 but 3.9.10 was found instead. Even though ...

unable to retrieve / interpret data from herdsmen using fetch

When sending a request to a node server, the server responds with headers and a blank body. Despite being able to view the headers in the network activity panel within dev-tools, I faced difficulties reading them using the following code: let uploaded ...

Apply CSS styles conditionally to an Angular component

Depending on the variable value, I want to change the style of the p-autocomplete component. A toggle input determines whether the variable is true or false. <div class="switch-inner"> <p [ngClass]="{'businessG': !toggle }" clas ...

The operation of multiplying values is not functioning properly in the output field

I'm currently working on a functionality where an output field needs to multiply its value based on the input entered into another field. For example, if the input field is set to 2, then the output field should display the result of multiplying that ...

Establishing specific categories for a universal element

I have been working on creating an input component that functions as a custom select for enums in my application. I have tried defining them for different types using concise one-liners but have run into various typing issues. Here is what I have so far: ...

Unexpected identifier error: Typescript interface name syntax is incorrect

I am currently learning Typescript and still navigating my way through it. I have extensively searched for a solution to the issue I am facing, but couldn't find one, hence I am seeking help here. The problem lies in a SyntaxError at the interface nam ...

The subsequent menu selection will be based on the chosen menu value

I am attempting to accomplish the following: https://i.sstatic.net/JffUWC02.png Essentially, I need a select options menu with labels where selecting an option will display a corresponding value. These values should then become labels for a second selec ...

Creating an ESNext JavaScript file and integrating it into an Angular 2 project: A comprehensive guide

I am facing an issue with my js file named UserService.js and source.js, which has been transformed using transformer typescript. My objective is to integrate this transformed js file into Angular. UserService.js import { Source } from "./source" ...

Is Angular equipped with named routes or states similar to UIRouter?

When working with AngularJS and ui-router, we define a state like this: .state('myState', { url: 'some-user-friendly-url' ... }) This allows us to easily specify the URL and name of the state. It simplifies navigation by letting ...

What are the best practices for establishing a secure SignalR client connection?

While tackling this issue may not be solely related to SignalR, it's more about approaching it in the most efficient way. In C#, creating a singleton of a shared object is achievable by making it static and utilizing a lock to prevent multiple threads ...

What is the best way to access a variable from script A within script B using TypeScript?

I am looking to extract a variable from another script in order to generate the subsequent section of the page based on this data. Below is the code snippet that retrieves data from an API: import Axios from "axios"; import React from "reac ...

Lack of MaterialUI Table props causing issues in Storybook

Currently, I am utilizing MaterialUI with some modifications to create a personalized library. My tool of choice for documentation is Storybook, using Typescript. An issue I have encountered is that the storybook table props are not consistently auto-gene ...

How can I display an ng-container or ng-template without using *ngIf?

I am trying to incorporate the tappable directive into the ion-card component within a custom component. I am using an @Input() myInputBool, similar to this: <ng-container *ngIf="myInputBool"> <ion-card> <ng-container r ...

Issue with npm resolution due to package requiring another third-party dependency

I'm encountering an issue with a requirement and I'm hoping for some assistance. I currently have a package called @unicoderns/orm that relies on mysql, which can be found at https://github.com/unicoderns/ORM Now I'm working on developing ...

The index access type cannot be used with T[Key extends keyof T]

My work frequently involves arrays structured like this: [ {key1: val1, key2: value2, key3: val3}, {key1: val1, key2: value2, key3: val3}, {key1: val1, key2: value2, key3: val3}] and I often need to convert them into a dictionary/map format, for example: ...

Enhancing Security with Subresource Integrity in Angular-Cli

Has anyone discovered a way to enable Subresource Integrity with Angular-CLI? I came across this GitHub Pull Request that suggests it may become a feature in the future: GitHub Pull Request. I tried to activate it on the current versions but had no luck. ...

Adjust the minimum time in ngb-timepicker in Angular version 4

I created a straightforward form for selecting a time range using the code snippet below: <form [formGroup]="editEventForm" (ngSubmit)="c(onSubmitUpdate())"> <div class="form-group bottom-form-details"> <div class="row"> < ...

Update your mappings for the city of Istanbul when utilizing both TypeScript and Babel

Currently, I am facing the challenge of generating code coverage for my TypeScript project using remap Istanbul. The issue arises due to the usage of async/await in my code, which TypeScript cannot transpile into ES5 directly. To circumvent this limitation ...