MatBlazor icon indicating copy to clipboard operation
MatBlazor copied to clipboard

MatSnackBar Content Not updating

Open BrettButcher opened this issue 5 years ago • 3 comments

I am trying to use MatSnackBar and have a variable for its content as follows:

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

The '@StatusMessage' is updated in the code behind:

 `    // 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 (MatDialogue)
}`

The StatusMessage in the 'div' works fine. However, it loads once in the snackbar but doesn't change after. Its as if the snackbar isn't getting re-rendered.

I have tried a few things such as trying 'CascadingParameter' on the StatusMessage.

I have a feeling this should 'just work', what am I missing? Many thanks.

BrettButcher avatar Jul 21 '20 11:07 BrettButcher

I had the same problem. I solved it with this workaround:

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

vivajohn avatar Jul 24 '20 07:07 vivajohn

Thanks for that @vivajohn ... I have worked around it now also, by using a different solution. (Blazorise) - I was having other issues, such as launching a snackbar from within the modal just before it closed down (to give a status update). That wouldnt work either, resulting in having to have a callback to the parent page to launch it. Just too much faff in the end when (so far) Blazorise did 'just work'. Thanks again for the tip.

BrettButcher avatar Jul 24 '20 09:07 BrettButcher

I had the same problem. I solved it with this workaround:

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

Beware, that will only work if you have some time between the Snacks! Because otherwise the Snackbar will not be hidden and therefore no rerendering and updating of the content will occur. I had a list of items where the first one should be displayed and my Bugfix was to declare the same Snackbar twice and let it show alternatively based on the list length.

MichaelMay81 avatar Mar 11 '21 14:03 MichaelMay81