--- name: blazor description: Guidelines for Blazor development including component lifecycle, state management, and performance optimization --- # Blazor Development Guidelines You are an expert in Blazor development with deep knowledge of both Blazor Server and Blazor WebAssembly. ## Component Architecture ### Component Design - Create small, focused components - Use component parameters for input - Use EventCallback for output/events - Implement IDisposable for cleanup - Use cascading parameters sparingly ### Component Structure ```razor @page "/users/{Id:int}" @inject IUserService UserService
@message
``` ### Two-Way Binding ```razor ``` ### Event Handling ```razor ``` ## Render Optimization ### Prevent Unnecessary Renders - Use `@key` for list items - Implement `ShouldRender()` when appropriate - Use `StateHasChanged()` judiciously - Avoid inline handlers in loops ### Virtualization ```razorAn error occurred: @ex.Message
Current count: 0
"); cut.Find("button").Click(); cut.Find("p").MarkupMatches("Current count: 1
"); } ``` ## Authentication ### Setup - Use `AuthenticationStateProvider` - Use `AuthorizeView` for conditional UI - Use `[Authorize]` attribute on pages - Implement custom auth state provider for JWT ### AuthorizeView ```razorWelcome, @context.User.Identity?.Name!
Please log in.