Skip to content

Commit

Permalink
Simpliy multiple state components example
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriyDurov committed Nov 8, 2024
1 parent 7a3a538 commit 940767d
Showing 1 changed file with 23 additions and 27 deletions.
50 changes: 23 additions & 27 deletions docs/src/03.state-hierarchy.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,24 @@ As you can see, the page's state is composed of the states of all its stateful c

When persisting the state of a component inside a state hierarchy, **Blazor.State** uses the component's type name as an identifier by default. This approach works well in cases where a single state parent does not contain multiple stateful components of the same type.

Consider the following example:
Consider the following example of how a page containing multiple instances of the same component might look:

```html
<MyComponent>
<MyComponent />
<MyComponent />
</MyComponent>
```csharp
// MyPage.razor
<MyComponent />
<MyComponent />
```

In this case, the page's state hierarchy should look something like the following:

```json
{
"n__MyComponent": {
"Text": "Some text",
"n__MyComponent": {
"Text": "Some text"
},
"n__MyComponent": {
"Text": "Some text"
}
"Text": "Some text"
},
"n__MyComponent": {
"Text": "Some text"
}
}
```
Expand All @@ -100,27 +97,26 @@ To solve this problem, **Blazor.State** introduces a component parameter **State

Consider the following example:

```html
<MyComponent>
<MyComponent StateId="NestedComponent1" />
<MyComponent StateId="NestedComponent2" />
</MyComponent>
```csharp
// MyPage.razor
<MyComponent StateId="NestedComponent1" />
<MyComponent StateId="NestedComponent2" />
```

In this case, the page's state hierarchy should now look something like the following:

```json
{
"n__MyComponent": {
"Text": "Some text",
"n__NestedComponent1": {
"Text": "Some text"
},
"n__NestedComponent2": {
"Text": "Some text"
}
"n__NestedComponent1": {
"Text": "Some text"
},
"n__NestedComponent2": {
"Text": "Some text"
}
}
```

As you can see, the state of the nested components is stored in the parent component's state, and the state of each nested component is prefixed with `n__` and suffixed with the component's **StateId**. This way, **Blazor.State** can distinguish between multiple components of the same type located under the same parent stateful component - and restore their states correctly, with each component receiving it's own state.
As you can see, the state of the nested components is stored in the parent component's state (in this example, the page is the parent component), and the state of each nested component is prefixed with `n__` and suffixed with the component's **StateId**. This way, **Blazor.State** can distinguish between multiple components of the same type located under the same parent stateful component - and restore their states correctly, with each component receiving it's own state.

You can use this knowledge to build state hierarchies of any complexity, including ones with multiple nested components of the same type, and **Blazor.State** will take care of persisting and restoring their states for you.

0 comments on commit 940767d

Please sign in to comment.