misterbee180
misterbee180

Reputation: 381

Easily trigger OnParametersSet on DynamicComponent Blazor

I have a dynamic component that displays various input.razor classes. I have a dropdown that I pull the input type from and adjust the DynamicComponent type

<DynamicComponent Type=ConfigComponentType @ref=ConfigComponent></DynamicComponent>

Once set, the OnParametersSet function for the component runs initially. Great~

If I swap to another component then swap back it runs again. Great~

But if I simply stay on this component, it doesn't call this again. Darn...

As you see, I don't pass any Parameters to the dynamicComponent so I can't easily update the parameters object. I've tried a lot of things including:

I'm trying to call OnParametersSet so that I can re-initialize my object w/ new object instances. Maybe there's another way to do this?

Does any one have any suggestions for how to easily trigger a refresh of my input components?

Upvotes: 0

Views: 1024

Answers (2)

Brian Parker
Brian Parker

Reputation: 14533

The issue is the render engine does not see the component as new. It can only go by its index and type, as none have changed it updates the existing component and hence it does not fire OnParametersSet

To solve the issue you need to use @key on the component so the render engine knows it is a new component regardless of its index.

@Key docs

Upvotes: 1

MrC aka Shaun Curtis
MrC aka Shaun Curtis

Reputation: 30016

Whatever you're doing within your " input.razor" components needs to be event driven. As you haven't shown much code, it's difficult to show you code that relevant to your question within make some wild assumptions, so here's some generic information.

If say your input controls are linked to a data record, then that record should be retrieved by a scoped data service. The service contains the current data record which you are displaying in various components. It also has a RecordChanged event that gets called whenever the record changes. Your input razor components register for the event and call StateHasChanged as required.

If you provide a bit more code, I can give you some relevant code. Or search the Internet for "Blazor notifier pattern" for various examples.

Upvotes: 0

Related Questions