2021-02-10 20:43:39 +08:00
---
2021-05-02 07:03:18 +08:00
no-loc: [Kestrel, appsettings.json, "ASP.NET Core Identity", cookie, Cookie, Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR]
2021-02-10 20:43:39 +08:00
---
2020-09-30 18:34:17 +08:00
Nested components typically bind data using *chained bind* as described in < xref:blazor / components / data-binding > . Nested and un-nested components can share access to data using a registered in-memory state container. A custom state container class can use an assignable < xref:System.Action > to notify components in different parts of the app of state changes. In the following example:
* A pair of components uses a state container to track a property.
2021-04-02 02:45:13 +08:00
* One component in the following example is nested in the other component, but nesting isn't required for this approach to work.
2020-09-30 18:34:17 +08:00
`StateContainer.cs` :
```csharp
2021-04-02 02:45:13 +08:00
using System;
2020-09-30 18:34:17 +08:00
public class StateContainer
{
2021-04-02 02:45:13 +08:00
private string savedString;
2020-09-30 18:34:17 +08:00
2021-04-02 02:45:13 +08:00
public string Property
2020-09-30 18:34:17 +08:00
{
2021-04-02 02:45:13 +08:00
get => savedString;
set
{
savedString = value;
NotifyStateChanged();
}
2020-09-30 18:34:17 +08:00
}
2021-04-02 02:45:13 +08:00
public event Action OnChange;
2020-09-30 18:34:17 +08:00
private void NotifyStateChanged() => OnChange?.Invoke();
}
```
In `Program.Main` (Blazor WebAssembly):
```csharp
builder.Services.AddSingleton< StateContainer > ();
```
In `Startup.ConfigureServices` (Blazor Server):
```csharp
2021-03-17 17:39:26 +08:00
services.AddScoped< StateContainer > ();
2020-09-30 18:34:17 +08:00
```
2021-04-02 02:45:13 +08:00
`Shared/Nested.razor` :
2020-09-30 18:34:17 +08:00
```razor
@inject StateContainer StateContainer
@implements IDisposable
2021-04-02 02:45:13 +08:00
< h2 > Nested component< / h2 >
2020-09-30 18:34:17 +08:00
2021-04-02 02:45:13 +08:00
< p > Nested component Property: < b > @StateContainer.Property< / b > < / p >
2020-09-30 18:34:17 +08:00
< p >
2021-04-02 02:45:13 +08:00
< button @onclick =" ChangePropertyValue " >
Change the Property from the Nested component
< / button >
2020-09-30 18:34:17 +08:00
< / p >
@code {
protected override void OnInitialized()
{
StateContainer.OnChange += StateHasChanged;
}
private void ChangePropertyValue()
{
2021-04-02 02:45:13 +08:00
StateContainer.Property =
$"New value set in the Nested component: {DateTime.Now}";
2020-09-30 18:34:17 +08:00
}
public void Dispose()
{
StateContainer.OnChange -= StateHasChanged;
}
}
```
2021-04-02 02:45:13 +08:00
`Pages/StateContainer.razor` :
2020-09-30 18:34:17 +08:00
```razor
2021-04-02 02:45:13 +08:00
@page "/state-container"
2020-09-30 18:34:17 +08:00
@inject StateContainer StateContainer
@implements IDisposable
2021-04-02 02:45:13 +08:00
< h1 > State Container component< / h1 >
2020-09-30 18:34:17 +08:00
2021-04-02 02:45:13 +08:00
< p > State Container component Property: < b > @StateContainer.Property< / b > < / p >
2020-09-30 18:34:17 +08:00
< p >
2021-04-02 02:45:13 +08:00
< button @onclick =" ChangePropertyValue " >
Change the Property from the State Container component
< / button >
2020-09-30 18:34:17 +08:00
< / p >
2021-04-02 02:45:13 +08:00
< Nested / >
2020-09-30 18:34:17 +08:00
@code {
protected override void OnInitialized()
{
StateContainer.OnChange += StateHasChanged;
}
private void ChangePropertyValue()
{
2021-04-02 02:45:13 +08:00
StateContainer.Property =
$"New value set in the State Container component: {DateTime.Now}";
2020-09-30 18:34:17 +08:00
}
public void Dispose()
{
StateContainer.OnChange -= StateHasChanged;
}
}
```
2021-02-09 21:04:07 +08:00
The preceding components implement < xref:System.IDisposable > , and the `OnChange` delegates are unsubscribed in the `Dispose` methods, which are called by the framework when the components are disposed. For more information, see < xref:blazor / components / lifecycle # component-disposal-with-idisposable > .