dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.55k stars 10.05k forks source link

Blazor global state one way binding. #52007

Open voroninp opened 1 year ago

voroninp commented 1 year ago

Is there an existing issue for this?

Is your feature request related to a problem? Please describe the problem.

I would like to have a more streamlined way of binding to the global state and reacting to its updates.

Currently recommended approach of working with global state is to expose events to which component attaches handlers and then detaches them in Dispose method.

@inject IState _state;
@implements IDisposable;

<div>
   @(_state.Date.ToString("yyyy-MM-dd"))
</div>

@code {
    protected override async Task OnInitializedAsync()
    {
        _state.Changed+= StateChanged;
    }

    public void Dispose()
    {
        _state.Changed -= StateChanged;
    }

    private void StateChanged()
    {
        // Maybe update internal component's state with values transformed from the changed global state,
       // If not used directly. 
        this.InvokeAsync(StateHasChanged);
    }
}

It's a working solution but adds some boilerplate code.

Describe the solution you'd like

Functionality similar to WPFs data binding would be fine, or binding to observables.

State can either expose events EventHandler <PropertyName>Changed; or use INotifyPropertyChanged interface.

Something like this:

@inject IState _state;
@implements IDisposable;

<div>
   @bind(_state.Date, d => d.ToString("yyyy-MM-dd"), default: "N/A") // or just '@bind(_state.Date)' if no transfomration is needed.
</div>

Additional context

No response

ghost commented 1 year ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.