I have one page on a Blazor Server app that has multiple child components and I could get some input about best practices about handling different routes, and state management for a somewhat complex page/component.
My page lookssimilar to the image below. Let's say I have the following components:
My question now is the following:
Should I create a new component with the @page "" attribute. Let's say "ProfileCalendarComponent" and include all the other previous components? (SearchDirectory, ProfileHeader, ProfileTabs)
I feel loading every other component every time I click on a tab is unnecessary and could eventually make the app slower since I will need to make trips to the DB (if not cached) every time a want to update a small portion of the page.
Is that considered best practice? or should I create a ProfileCalendarComponent that only contains what's going in the content area?
I feel there should be an easier way to achieve this, meaning saving the state and allowing to get to that state by URL. Please advise
After a few days of trying different things I will post my solution:
To make the tabs work: Every time I click on a tab option. I fire an event to the parent component (DirectoryComponent), I set a variable in the parent component "string SelectedTab" and in the content area I have a switch statement that loads a different child component based on "SelectedTab"
How do I access a specific tab by URL?: This was actually quite simple. My DirectoryComponent (main component) has multiple routes defined (@page). For example:
@page "/directory"
@page "/directory/{Id:int}/{SelectedTab}"
This way if I want to go directly to the calendar I can have a link to "/directory/111/calendar" and the DirectoryCompontent will automatically fill out the SelectedTab parameter and load the right Child Component.
I am also cascading this parameter to the ProfileTabsComponent so I can mark the right tab as "active".
I wrapped the ProfileHeaderComponent, ProfileTabsComponent, and the switch in an if statement so it only displays if the Id parameter is set to a value. Otherwise I display a message saying "Select a person from the directory".
I feel pretty confident with this but I would still like to see what other people think.
I hope this helps other people as well.