How to pass data from child component to parent

Display name

I can't seem to find any answer for this question but it seems simple enough to exist. Basically I want to send data from Child component to Parent.

Here is an example of Child component Cars

@using Newtonsoft.Json

@if(ReponseCarsJSON) {
  @foreach (var car in ReponseCarsJSON) {
    <p @onclick="() => onSelectCar(car.id)"> model: @car.model year: @car.registration_year </p>
    }
}


@code{
  [Parameter]
  public EventCallback<string> CarSelected { get; set; }

  async Task onSelectCar(string e) {
    await CarSelected.InvokeAsync(e);
  } 

  //ReponseCarsJSON = ... json stuff to fetch Cars API
}

My question is how do I retrieve this data on parent, for example:

<Cars CarSelected="@getCarId"> </Cars>

@code {
  private void getCarId () {
    // get car id here
  }
}
Lex

Simply pass it as a parameter to your getCarId method. i.e.:

@code {
    private void getCarId(string carId) {
        // use carId here
    }
}

One thing I like to do to avoid any potential errors is to make sure there's actually a callback delegate registered. So I do:

private async Task onSelectCar(string e) {
    if (CarSelected.HasDelegate) {
        await CarSelected.InvokeAsync(e);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to pass data from Child to Parent, if Child component dynamic component

How to pass async data to child component from parent component?

How to pass data to child component from parent vuejs

How to pass data from child component to parent [Angular]

How to Pass data from child to parent component Angular

How to pass data from parent to child component in Angular 4

How to pass data from child to parent component using react hooks

How to pass data from child component to its parent in ReactJS?

How to pass single data from child to parent component?

How to pass multiple data from child to parent component in Angular?

How to pass data from child to parent component in React.JS?

VueJS - How to pass Axios response data from parent to child component?

How to pass form data from parent component to child?

How to pass data from Child function to parent component in react

How to pass data from child component to parent component when button clicked on parent component

React | pass data from parent to child component

Pass data object from parent to child component

Pass data from child component to parent

Cannot pass data from Parent to Child component

How to pass value from child to parent component

How to pass a value from a child to a parent component?

how to pass an event from a child component to a parent?

Pass data from child to parent and to another child component React Js

Pass data from parent component to child of child in Vue js

Pass data from Parent Component to nested Child Component in Angular

Pass data via on click from child component to parent component react

filtered data not getting pass from parent component to child component in React

How pass data from the child component (the child has its own state) to the parent?

How to pass data from parent component to child component in vue composition API?