{"id":1506,"date":"2025-06-27T10:48:22","date_gmt":"2025-06-27T10:48:22","guid":{"rendered":"https:\/\/www.cmarix.com\/qanda\/?p=1506"},"modified":"2026-02-05T12:05:43","modified_gmt":"2026-02-05T12:05:43","slug":"how-do-you-pass-data-between-blazor-components","status":"publish","type":"post","link":"https:\/\/www.cmarix.com\/qanda\/how-do-you-pass-data-between-blazor-components\/","title":{"rendered":"How Do you Pass Data Between Blazor Components?"},"content":{"rendered":"\n<p>Blazor allows components to communicate with each other in different ways, depending on how they are related. You can pass data from a parent to a child, notify a parent of an action from the child, or even share data across unrelated components. Understanding these methods helps you build clean, maintainable applications in Blazor.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Different Ways to Pass Down Data Between Blazor Components<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Using Parameters (Parent to Child)<\/h3>\n\n\n\n<p>The most common way to pass data.<\/p>\n\n\n\n<p><strong>ParentComponent.razor<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@page \"\/parent\"\n&lt;h3>Parent Component&lt;\/h3>\n&lt;ChildComponent Message=\"Hello from Parent!\" \/><\/code><\/pre>\n\n\n\n<p><strong>ChildComponent.razor<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;h4>Child Component&lt;\/h4>\n&lt;p>@Message&lt;\/p>\n\n@code {\n    &#91;Parameter]\n    public string Message { get; set; }\n}<\/code><\/pre>\n\n\n\n<p><strong>Explanation: <\/strong>The parent passes a value to the child via the Message parameter.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Using EventCallback (Child to Parent)<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Passing Data from Child to Parent using EventCallback<\/h4>\n\n\n\n<p>Useful when the child component needs to notify the parent of an action.<\/p>\n\n\n\n<p><strong>ParentComponent.razor<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;h3>Parent&lt;\/h3>\n&lt;p>Received: @ChildData&lt;\/p>\n&lt;ChildComponent OnDataSent=\"ReceiveDataFromChild\" \/>\n\n@code {\n    private string ChildData;\n\n    private void ReceiveDataFromChild(string data)\n    {\n        ChildData = data;\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>ChildComponent.razor<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;button @onclick=\"SendData\">Send Data to Parent&lt;\/button>\n\n@code {\n    &#91;Parameter]\n    public EventCallback&lt;string> OnDataSent { get; set; }\n\n    private async Task SendData()\n    {\n        await OnDataSent.InvokeAsync(\"Hello Parent!\");\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Explanation<\/strong><strong>: <\/strong>EventCallback allows invoking a method defined in the parent from the child.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Using Cascading Values and Parameters<\/h3>\n\n\n\n<p>Great for passing data to deeply nested components without manually passing props.<\/p>\n\n\n\n<p><strong>App.razor or a Parent Component<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;CascadingValue Value=\"AppTitle\">\n    &lt;NestedComponent \/>\n&lt;\/CascadingValue>\n\n@code {\n    string AppTitle = \"Blazor Blog App\";\n}<\/code><\/pre>\n\n\n\n<p><strong>NestedComponent.razor<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;p>Cascading Value: @Title&lt;\/p>\n\n@code {\n    &#91;CascadingParameter]\n    public string Title { get; set; }\n}<\/code><\/pre>\n\n\n\n<p><strong>Explanation: <\/strong>Useful for passing themes, user info, or localization strings down the component tree.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. Using a Shared Service (Unrelated Components or Sibling Communication via State)<\/h3>\n\n\n\n<p>Shared state or data across unrelated components.<\/p>\n\n\n\n<p><strong>AppState.cs (Service)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class AppState\n{\n    public string SharedMessage { get; set; }\n\n    public event Action OnChange;\n\n    public void SetMessage(string message)\n    {\n        SharedMessage = message;\n        NotifyStateChanged();\n    }\n\n    private void NotifyStateChanged() => OnChange?.Invoke();\n}<\/code><\/pre>\n\n\n\n<p><strong>Program.cs (register the service)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>builder.Services.AddScoped&lt;AppState>();<\/code><\/pre>\n\n\n\n<p><strong>SenderComponent.razor<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@inject AppState AppState\n\n&lt;input @bind=\"Message\" \/>\n&lt;button @onclick=\"SendMessage\">Send&lt;\/button>\n\n@code {\n    private string Message;\n\n    private void SendMessage()\n    {\n        AppState.SetMessage(Message);\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>ReceiverComponent.razor<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@inject AppState AppState\n\n&lt;p>Received Message: @AppState.SharedMessage&lt;\/p>\n\n@code {\n    protected override void OnInitialized()\n    {\n        AppState.OnChange += StateHasChanged;\n    }\n\n    public void Dispose()\n    {\n        AppState.OnChange -= StateHasChanged;\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>Explanation<\/strong>: This is similar to a global event bus great for managing state or broadcasting data changes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. Using .NET Events or Delegate Actions<\/h3>\n\n\n\n<p>You can pass delegate methods directly between components.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!-- ParentComponent.razor -->\n&lt;ChildComponent ActionCallback=\"@DoSomething\" \/>\n\n@code {\n    private void DoSomething()\n    {\n        Console.WriteLine(\"Called from child!\");\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!-- ChildComponent.razor -->\n&lt;button @onclick=\"ActionCallback\">Call Parent Method&lt;\/button>\n\n@code {\n    &#91;Parameter]\n    public Action ActionCallback { get; set; }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Method<\/strong><\/td><td><strong>Direction<\/strong><\/td><td><strong>Use Case<\/strong><\/td><\/tr><tr><td><strong>[Parameter]<\/strong><\/td><td>Parent \u2192 Child<\/td><td>Passing data down<\/td><\/tr><tr><td><strong>EventCallback<\/strong><\/td><td>Child \u2192 Parent<\/td><td>Triggering parent methods<\/td><\/tr><tr><td><strong>CascadingParameter<\/strong><\/td><td>Parent \u2192 Deep Child<\/td><td>App-wide data (e.g., user, theme)<\/td><\/tr><tr><td><strong>Shared Service<\/strong><\/td><td>Any \u2194 Any<\/td><td>Global\/shared state (siblings, global)<\/td><\/tr><tr><td><strong>Delegate \/ Action<\/strong><\/td><td>Flexible<\/td><td>Passing logic as parameters<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Blazor provides a flexible and powerful component communication system. By using the right technique for the right scenario, you can ensure your Blazor apps remain modular, maintainable, and performant whether it&#8217;s a dashboard, a blog, or a complex enterprise portal.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Blazor allows components to communicate with each other in different ways, depending on how they are related. You can pass data from a parent to a child, notify a parent of an action from the child, or even share data across unrelated components. Understanding these methods helps you build clean, maintainable applications in Blazor. Different [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1511,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[4,3],"tags":[],"class_list":["post-1506","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dot-net","category-web"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/1506","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/comments?post=1506"}],"version-history":[{"count":9,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/1506\/revisions"}],"predecessor-version":[{"id":1517,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/posts\/1506\/revisions\/1517"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media\/1511"}],"wp:attachment":[{"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/media?parent=1506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/categories?post=1506"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cmarix.com\/qanda\/wp-json\/wp\/v2\/tags?post=1506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}