0

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:

  • Making the ComponentType null -- nothing
  • Hiding the Dynamic component on the UI -- just blinks
  • Calling StateHasChanged --nothing
  • Calling ConfigComponent.SetParametersAsync(new ParameterView()) -- throws an error: Object reference not set to an instance of an object

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?

7
  • What part of your application is going to trigger the resfresh and when will it do this - important information for determining where you need to change code. Commented Nov 7, 2021 at 9:41
  • 1
    If it is the parent you can simply make a public method in DynamicComponent and call that through the reference you already have in ConfigComponent Commented Nov 7, 2021 at 9:43
  • 1
    Try using a different @key so the renderer engine knows its a new instance... Commented Nov 8, 2021 at 1:45
  • 1
    > The @key directive allows instructing Blazor to use a specific key to compare elements instead of using the index. Blazor will compare the existing items with the new ones using the value of the key. This way it will better detect additions/modifications/deletions. In this example, you can see that only the new li is inserted. Commented Nov 9, 2021 at 0:22
  • 1
    Yo, this key seems to be the answer! Changing it seems to trigger a new instance of dyncamicComponent. Thanks @BrianParker Commented Nov 10, 2021 at 1:06

2 Answers 2

1

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

0

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.