I have a blazor page with different components.
Component structure is the following:
page
component1
component11
component2
When event happens on component11, component2 should update it's state.
On the same component I use OnStateChange event.
For update state of parent component, I can use EventCallback too.
But here I need to pass event two levels up and then down to other component.
Looks a little bit complex. Maybe exist some easy way to do this.
Can event bubbling work automatically? Maybe some application settings?
You need to use some form of notification pattern.
Here's a simple provider. Note it requires the
WeatherForecastServicefrom DI to demo injecting services.You can either register this as a scoped DI service or, if you're using it within the scope of a page create an instance in the page component and cascade it.
Next a few components that use
TimeStampProvider.TimeStampSetter
TimeStampViewer
MyComponent
And final the page, with lots of sub-components to keep in sync. Note the
fixedcascading value.Test it and you'll see everything stays in sync!