What are the different ways to pass data from a parent component to a child component?
Last updated
The primary way is component parameters, but Blazor offers several mechanisms depending on whether the data is direct input, shared markup, or shared across a subtree.
Component parameters:
Public properties marked
[Parameter]set as attributes:<Child Title="Hi" />.The most common and explicit path.
Child content: Pass markup between the child's tags via a
RenderFragment ChildContentparameter.Cascading values: Use
<CascadingValue>and[CascadingParameter]to flow data down a subtree without threading it through every level.Attribute splatting: Forward extra attributes via
CaptureUnmatchedValuesand@attributes.Shared services (indirect): An injected state container/service both components read, useful when the relationship isn't strictly parent-to-child.
- https://www.telerik.com/blogs/blazor-basics-component-parameters-state-management
- https://medium.com/@quentinsims89/blazor-component-communication-mastering-parameters-and-cascading-parameters-6ac093023956
- https://medium.com/@dusan.velimirovic/handling-blazor-component-model-mastering-parameters-and-lifecycle-methods-f9896ba68c2c
- https://www.learnblazor.com/components
- https://learn.microsoft.com/en-us/aspnet/core/blazor/components/templated-components?view=aspnetcore-10.0
- https://www.youtube.com/watch?v=o9fQ1cLvNlo
- https://learn.microsoft.com/en-us/aspnet/core/blazor/components/data-binding?view=aspnetcore-10.0
- https://chrissainty.com/3-ways-to-communicate-between-components-in-blazor/