Closed Stamo-Gochev closed 1 year ago
I have looked at this and can see page rendering (only the first time) with the correct cascading value, then resets and shows the default. It appears that the page is rendering twice.
Here are my test cases (simplified code):
You can watch what is happening by navigating to the \counter
page in each project.
We have the same issue in Radzen.Blazor - a CascadingParameter is null in the child because of the new static rendering mode (I presume).
@Stamo-Gochev Fun times ahead for us Blazor UI component developers ;) Those new blazor interactivity modes will make us cry for sure.
@akorchev yeah, the CascadingParameter
is set then is null. You can see it in the above sample projects. Hopefully addressed in RC2.
@Stamo-Gochev I'm afraid cascading parameters do not pass across rendermode boundaries. There are two main aspects to understanding this:
PersistentComponentState
is serialized and recovered automatically (if it's JSON-serializable, otherwise it's an error)We chose not to JSON-serialize cascading parameters because the typical usage patterns for cascading parameters are somewhat like DI services. There are often platform-specific variants of cascading parameters, so it would be unhelpful to developers if we stopped them from having Server-interactive specific versions or WebAssembly-specific versions. And many cascading parameter values in general are not serializable, so it would be impractical to update existing apps if you had to stop using all nonserializable cascading parameter values. It wouldn't even help in your case since your cascading parameter values are definitely not serializable.
If you need to make state available to all interactive components as a cascading parameter, you'll need to use the new API we've added to enable this. In Program.cs
, it's now possible to register cascading parameters in the same way as registering DI services. For example:
// There are also various other overloads, e.g., if you need to use a factory pattern or to emit updated values later
builder.Services.AddCascadingValue(someValue);
Such values will then be available to all components, including interactive ones, since they go through the same DI container setup. Likewise you can call this from Program.cs
in WebAssembly projects if you need.
As a component library author, you could create an extension method like:
builder.Services.AddTelerikCascadingParameters();
... and instruct developers to call that, as an alternative to telling them to add your <RootComponent>
component in MainLayout.razor
. Then it would work everywhere. I appreciate that causes some extra work for you as you will need to take a different approach - sorry for the inconvenience! But it's unavoidable (as far as I can imagine) since in general it wouldn't be possible to serialize component instances across server boundaries.
Does that make sense?
Thanks for the info and the recommendations, @SteveSandersonMS, we will consider the suggestions. I do understand that this is actually by design taking into account the interactivity-related specifics.
This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.
See our Issue Management Policies for more information.
Is there an existing issue for this?
Describe the bug
When a Blazor component is used in
MainLayout.razor
in a Blazor app with Blazor Server interactive render mode, passing this component as a cascading parameter results in the component having the valuenull
.Details
The Blazor app renders a RootComponent, which basically re-renders its child content. The component wraps the
@Body
element inside MainLayout.razor and it is used together with a ContentComponent that appears on the Home.razor page.When the app is run, the first time the
ContentComponent
is initialized, theRootComponent
has its correct value, but on consecutive calls it becomesnull
.Expected Behavior
The component that is passed down the hierarchy as a cascading parameter should not be null.
Steps To Reproduce
RootComponent
value becomesnull
or see the attached GIFExceptions (if any)
The cascading value is
null
..NET Version
8.0.100-rc.1.23455.8
Anything else?