Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Events between two components #1714
I have two components:
one selectlist and one "View"
The view is populated from datas depending on which element is selected in the select list.
I seen It's possible to raise events from parents to children and inverse, but How to raise events between two components who are not parented ?
In an SPA application, as far as I got it, there is a single Html page; in Blazor this page, named Index.Html, residing in the wwwroot folder. There is also a root Component, currently named Main, which is rendered into the Index page. The Main Component may contain the rest of your Components hierarchically: Think of a tree view. So you see all the Components are begotten or parented by the Main Component, They all belong to the same family of Blazor Components, and are all related to each other directly or indirectly.
Best resources to understand communication between Components in Blazor:
Hope this helps... if you would need specific help, please show code...
You can use ref to capture both component references, you will invoke event when change happens, subscribe for this event and call second component's public method to update view. Or you can save in some static class bot components instances inside onload and invoke public methods when you need using static class. You can use many ways for that goal (state, services and etc) key is here event raising and component instances to access them.
I've done this in my JWT/LOB example by using my ApplicationState singleton as a mediator.
Basically I have a function in my Appstate, and I have a public event delegate.
Since most parts of my app, inject my appstate class to check for things like logged in status, anything that wants to know when a successful login is made, attaches to the LoginSuccessful event in the class.
When something logs in, it calls Login on the AppState class, and the app state then raises it's own LoginSuccessful event, which any interested listeners respond to.