Hi @mythz
I’m trying to implement the CascadingAppState into your tailwind balzor templates and I’m running into an issue if it being null and the OnAfterRenderAsync()
lifecycle event in the CascadingAppState.razor.cs
never fires.
Basically, I’ve copied the five files from the AppState project into your template and adjusted namespaces.
- AppState.cs
- IAppState.cs
- CascadingAppState.razor
- CascadingAppState.razor.cs
- StatePropertyChangedArgs.cs
I then added the CascadingAppState to the routes.razor page like so:
<CascadingAppState>
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(Layout.MainLayout)">
<NotAuthorized>
<RedirectToLogin/>
</NotAuthorized>
</AuthorizeRouteView>
<FocusOnNavigate RouteData="@routeData" Selector="h1"/>
</Found>
</Router>
</CascadingAppState>
and added AppState to the Counter.razar page like so:
@page "/counter"
@rendermode RenderMode.InteractiveServer
<PageTitle>Counter</PageTitle>
<Heading1>Counter</Heading1>
<!-- I had to add the ?. otherwise the null here breaks the page --!>
<p class="my-4">Current count: @AppState?.Count</p>
<PrimaryButton @onclick="IncrementCount">Click me</PrimaryButton>
@code {
[CascadingParameter]
public CascadingAppState AppState { get; set; }
private int currentCount = 0;
private void IncrementCount()
{
AppState.Count++; // This is null when I hit here
}
}
While I can get this to work in other projects, for some reason with your template it does not work as expected. I’ve tried the SSR and the WASM templates and both experience the same behavior.
Observations:
I do see lifecycle events firing and if I refresh and watch closely, I can see that it does have a value for a quick second, before it goes null.
By time I click the button and hit the breakpoint on the AppState.Count++
, AppState is null.
The CascadingAppState example just works, and if I start a new project, it seems to work no problem.
What am I missing here?