MatSnackBar Content (Blazor) Not updating

416 views Asked by At

I have seen this question, but it doesn't really get answered: MatSnackbar not updating MatSnackBarContent I have a similar issue and I wanted to see if anyone knows a solution.

I have a simple example:

     <div>Snackbar Status Message =  @StatusMessage</div>  <<< This shows the actual value
    
    <MatSnackbar @bind-IsOpen="@statusBarIsOpen">
        <MatSnackbarContent   >@StatusMessage</MatSnackbarContent>   <<< This should show the same
    </MatSnackbar>

I am updating the @StatusMessage by a custom event callback on a user form, and want to show the success or not of the update. (Quite a common use case I would have imagined?)

    // This is the callback handler for the 'OnUserSaved' callback
    protected void UserSaved(string statusmessage)
    {
        StatusMessage = statusmessage;     <<< Update the status message
        statusBarIsOpen = true;            <<< Open the snackBar / status bar
        this.StateHasChanged();            <<< fire the changed event
        isOpened = false;                  <<< this closes the form
    }

The first time, the StatusMessage is rendered correctly. After that it just stays fixed at the first message. While the above correctly shows the status. I mean yes I could just use the line above and clear it etc.. but then, what is MatSnackBar for? Is there some clear function I am missing, or am I just doing it wrong?

1

There are 1 answers

1
Felipe Roman On

A solution on github solved with this workaround:

@if(@statusBarIsOpen)
{
    <MatSnackbar @bind-IsOpen="@statusBarIsOpen">
        <MatSnackbarContent>@StatusMessage</MatSnackbarContent>
    </MatSnackbar>
}