-
Notifications
You must be signed in to change notification settings - Fork 10k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to access parent component from child component. #15651
Comments
If you want parent to get notified about child change or child to do something with parent, you can do it with different ways: 1. Child will have action and parent will subscribe on it, child can invoke this action when we need parent to notify, parent subscribed callback will fire and will do parent side stuff.
2. You can pass parent instance reference as parameter to child or you can save parent instance reference in some static class variable and then access and manipulate it from child. In my opinion first approach is better if you have small to do with parent, second one is better if you have lot to do with parent and from different places inside or outside child component. Using static variables is often helpful and I do it, you can access them from anywhere you like, you can bind component properties to it and get components refreshed with calling StateHasChanged from outside with saved instance reference. (if it is confusing I can show how it works). But it has some drawbacks too. We can use different approaches to achieve our goals, with one will be qualified as best practice will show future. Hope this was helpful, good luck with your coding. |
Thank you very muck. |
You are welcome :) I think will be good if we change issue name another people to find it useful too. My suggestion will be - How to access parent component from child component. |
👯♂️ |
You can ask questions here for more quick replies. |
I tried to implement your proposed solution (approach 1), along with two solutions based on your suggestions (approaches 2A and 2B) and my original solution (approach 3). Approach 1ChildComponent
ParentComponent
This code generate a warning due the event CounterChanged being public. warning BL9993: Component parameter 'CounterChanged' is marked public, but component parameters should not be public. Approach 2ABased on you suggestions, I coded a first implementation ChildComponent
ParentComponent
This implementation has a problem: Parent type is fixed for the component, that make the code not reusable. Approach 2BChildBase
ChildComponent
ParentComponent
Approach 3This is the solution using a property and an event both passed to the Child as parameter. ChildComponent
ParentComponent
Conclusions:I prefer to avoid having warnings in my code, so I have to exclude approach 1. |
It's a known issue (#610) that will eventually be fixed. |
Sorry I was not able to reply, now also have not time but Sunday evening I will show my samples for this scenario. |
Warning that component property or action is public not makes sense and should be removed because we often split markup and logic, so if we not make properties or actions public markup can't see them! Hope team will remove this warning or tell us how can be solved above mentioned case. If no one will reply I will open separate issue about this no sense warning. Another part needs more time to answer, maybe later :) |
Use |
Protected helps in this case but there are another cases too where it is not helpful, I opened new Issue - 1388. Thank you for answer. |
@aviezzi For example:
Chessengine has compsettings property wich has all component references
some method in chessengine
call examples from different places
I can save this instance reference in some static class too if I have only one component in my page (in chess case I have two components on page which play between each other, so I save component instances in chessengine instances not in static class):
Use example
Sometimes I link static property to component property because I use this value in many places and I want to access it as static value outside component rather than components property.
I hope it makes sense for you and is clear. This tricks helps me to do what I want achieve. Good look :) |
I need to do something very similar to what is described in https://blazor.net/docs/components/index.html,
section [Data Binding], subsection [Component Parameters], with the only exception that I need to update the parent component from the child, so I first moved the button to the latter. The implementation is:
ParentComponent
ChildComponent
But this approach doesn't work. So I tried to bind the event and value directly (see the code below), but it still fails, the only improvement is that now I can see the "I was called" line in the console output.
ParentComponent
ChildComponent
Then I added manualy UI refresh
ParentComponent
Child Component
The last thing I tried was to add a manual UI refresh via StateHasChanged(), this works, but I don't understand why the forced refresh is necessary, as the bind-{property} syntax should perform a Two-Way binding already. Is this behaviour intended or is it a bug?
Thanks and regards,
Alberto
The text was updated successfully, but these errors were encountered: