-
-
Notifications
You must be signed in to change notification settings - Fork 363
fix(Tab): should not rerender tabitem after close #6294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Co-Authored-By: Ockyd007 <[email protected]>
This reverts commit 25b6158.
Reviewer's GuideThis PR prevents unintended rerendering of TabItems upon closing by stabilizing component keys, wrapping each TabItem in its own container with explicit keys and class names, and streamlining CSS and lookup logic. Class diagram for updated Tab and TabItemContent componentsclassDiagram
class Tab {
+List<TabItem> TabItems
+RenderTabItem(TabItem item)
+static GetTabItemClassString(TabItem item) string
}
class TabItemContent {
-Guid _key
+Render()
-RenderContent()
-BuildRenderTree(RenderTreeBuilder builder)
-RenderItemContent(RenderFragment? content)
}
Tab --> TabItemContent : uses
TabItemContent --> TabItem : uses
class TabItem {
+string Id
+bool IsActive
+RenderFragment? ChildContent
}
Flow diagram for TabItem rendering and rerender preventionflowchart TD
A[TabItems collection] -->|Find active| B[TabItem]
B --> C{IsActive?}
C -- Yes --> D[Wrap in div with key and class]
D --> E[RenderTabItem]
C -- No --> F[Skip or render with d-none class]
E --> G[TabItemContent]
G --> H[Render content with stable key]
H --> I[Display TabItem content]
F --> J[Hidden TabItem]
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @ArgoZhang - I've reviewed your changes - here's some feedback:
- Regenerating a new Guid for
_keyon everyRender()will remount the content each cycle—if you only want to force a remount on close, consider updating_keyonly whenItem.IsActiveactually changes. - You’re using both
@key="@item"on the wrapper andbuilder.SetKey(_key)insideTabItemContent; choosing a single stable key (for example,item.Id) will simplify reconciliation and make the intent clearer. GetTabItemClassStringis declared to returnstring?but always returns a non-null string—removing the nullable annotation will better reflect its actual behavior.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Regenerating a new Guid for `_key` on every `Render()` will remount the content each cycle—if you only want to force a remount on close, consider updating `_key` only when `Item.IsActive` actually changes.
- You’re using both `@key="@item"` on the wrapper and `builder.SetKey(_key)` inside `TabItemContent`; choosing a single stable key (for example, `item.Id`) will simplify reconciliation and make the intent clearer.
- `GetTabItemClassString` is declared to return `string?` but always returns a non-null string—removing the nullable annotation will better reflect its actual behavior.
## Individual Comments
### Comment 1
<location> `src/BootstrapBlazor/Components/Tab/Tab.razor:36` </location>
<code_context>
foreach (var item in TabItems)
{
- @RenderTabItem(item)
+ <div @key="@item" id="@item.Id" class="@GetTabItemClassString(item)">
+ @RenderTabItem(item)
+ </div>
</code_context>
<issue_to_address>
Adding @key to the div may affect Blazor's diffing behavior.
If item is a complex object, property changes may not trigger updates as expected. Using a unique, stable value like item.Id as the key is recommended.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| foreach (var item in TabItems) | ||
| { | ||
| @RenderTabItem(item) | ||
| <div @key="@item" id="@item.Id" class="@GetTabItemClassString(item)"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Adding @key to the div may affect Blazor's diffing behavior.
If item is a complex object, property changes may not trigger updates as expected. Using a unique, stable value like item.Id as the key is recommended.
Link issues
fixes #6271
fixes #6293
Summary By Copilot
Regression?
Risk
Verification
Packaging changes reviewed?
☑️ Self Check before Merge
Summary by Sourcery
Prevent tab items from re-rendering on close by stabilizing component keys and isolating content in wrapper divs.
Bug Fixes:
Enhancements: