Best Practice passing object to component blazor

21 hours ago 1
ARTICLE AD BOX

I need advice or an idea how this can be improved - what is the correct (or better) way to passing a large object?

I have a page called Tracker.razor. This page was specifically only for grabbing data from a database.

I also have Map.razor which is a component where I call Google maps.

How can I call the component from Tracker.razor?

<main class="main-content col p-2 d-flex flex-column order-2 order-md-2"> <Utility.Map GoogleRouteResult="GoogleDataResult" ValueData="data" /> </main>

Tracker.razor.cs:

public partial class Tracker.razor.cs { private MyBookingData bookingData; private MyBookingData data; protected override async Task OnAfterRenderAsync(bool firstRender) { if (firstRender) // This is where I grab the MyBookingData bookingData } // This function being called for every 30 sec private GetBookingData() { // Grab MyBookingData bookingData JobStatus // if success then assign this to data // data = bookingData // Note i did this because some reason bookingData changes wont // get recognize by blazor. } }

Here's how I set the Map.razor.cs:

public partial class Map { [Parameter] public MyBookingObject ValueData{ get; set; } [Parameter] public EventCallback<GoogleRouteModel> GoogleRouteResult { get; set; } protected override async Task OnParametersSetAsync() { try { if (ValueData is null) return; // My Logic to check if there's changes from ValueData to tempData // if yes then assign tempData = ValueData // If not then return; // My entire Logic } } }

This is working as intended, but I just want to know if there is a better way of doing it?

Because I only need to update the data / pass the data to the map every 30 seconds, while if I use OnParameterSetAsync, it keeps calling every time, hence why do I have a logic to detect if there's a changes between 2 of same object.

I also happen to consider to having creating an event Action<MyBookingData>, but I'm not sure if this wouldn't cause trouble in the future.

Any help will be appreciated. Thank you in advance

Read Entire Article