Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Blazor persistentstate allowupdates

$
0
0
@page "/weather"@rendermode InteractiveServer@inject CustomUserService UserService<PageTitle>Weather</PageTitle><h1>Weather</h1><p>This component demonstrates showing data and persistent state.</p><p>User service hit count (increments each circuit init / interaction): <strong>@UserService.HitCount</strong></p>@if (!string.IsNullOrEmpty(UserService.UserName)){<p>Current user name from persistent service: <strong>@UserService.UserName</strong></p>}else{<p>No user name set in persistent service.</p>}@if (Forecasts == null){<p><em>Loading...</em></p>}else{<button class="btn btn-sm btn-secondary mb-2 ms-2" @onclick="Increment">Increment Hit Count</button><table class="table"><thead><tr><th>Date</th><th aria-label="Temperature in Celsius">Temp. (C)</th><th aria-label="Temperature in Fahrenheit">Temp. (F)</th><th>Summary</th></tr></thead><tbody>            @foreach (var forecast in Forecasts)            {<tr><td>@forecast.Date.ToShortDateString()</td><td>@forecast.TemperatureC</td><td>@forecast.TemperatureF</td><td>@forecast.Summary</td></tr>            }</tbody></table>}@code {    // Demonstrates component-level persistent state. After prerender or circuit reconnect, the last snapshot is restored.    // AllowUpdates enables persisting updated values during interactive rendering.    // [PersistentState(AllowUpdates = true)]    // [PersistentState(RestoreBehavior =RestoreBehavior.SkipInitialValue)]    //    [PersistentState(RestoreBehavior = RestoreBehavior.SkipLastSnapshot)]    // [PersistentState]    public WeatherForecast[]? Forecasts { get; set; }    protected override async Task OnInitializedAsync()    {        var startDate = DateOnly.FromDateTime(DateTime.Now);        var summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" };        await Task.Delay(600);        Forecasts ??= Enumerable.Range(1, 5).Select(index => new WeatherForecast        {            Date = startDate.AddDays(index),            TemperatureC = Random.Shared.Next(-20, 55),            Summary = summaries[Random.Shared.Next(summaries.Length)]        }).ToArray();    }    private void Increment()    {        UserService.HitCount++;    }    public class WeatherForecast    {        public DateOnly Date { get; set; }        public int TemperatureC { get; set; }        public string? Summary { get; set; }        public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);    }}

I have this example to trying out the newest blazor bits. I am trying to wrap my head around how the new persistentstate attribute works. I understand that when you do full reloading it is getting the state to the client to do not 2 times fetch but what makes the difference with allowupdates=true . If I do internal navigation then I do not have a flash because there is no prerendering but the documentation says something like "it is for enhanced navigation". Could someone help me out how could I demonstrate it? The documentation is not so clear what exactly is happening.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>