(Blazor) I'm having trouble accessing a variable inside a parent component.
I'm trying to access a simple bool in a parent component that shows when my side bar has been collapsed. I'm using the Blazor Fluent UI components by the way.
Okay so I've got this FluentNavMenu:
Then I've got a parent component:
That binding sets the
SideBarExpanded
every time I open or close it. I need to access it in a parent component to hide something else. What's the best way to detect that this variable has changed in the parent?
There is also a component attribute I can add to the FluentNavMenu
called ExpandedChanged
which takes in an EventCallback
, but I have no idea how to use it in the context of this particular component.
I've watched this video around 10 times and I still can't wrap my head around it as this particular scenario seems different.
Thanks!1 Reply
The
Expanded
, and ExpandedChanged
parameters are the two halves of the two-way binding that you are setting up with @bind-Expanded
.
From what I understand from your explanation, you want to have a wider scope of access to the state of the Sidebar than you currently do. Are you persisting this value anywhere, i.e. as a cookie, or in local storage?
What you are describing is "State Management". This is a hot topic within any web stack, and there are many ways to achieve it. There are three main objectives with state management:
1. Setting the state
2. Getting the state
3. Persisting the state
You can use local storage to persist the state; bearing in mind that you don't have access to local storage until OnAfterRenderAsync
.
To get/set the state, you can use a CascadingValue
component that you then call in the children with [CascadingParameter]
.
AppStateProvider.razor
AppState.cs
DISCLAIMER: This is written in Discord, and is untested, there may be syntax errors.
Consumer.razor
That shows how to use, set, and store, using cascading state management.