Skip to content

Commit

Permalink
finished up GetDisplayItems alternative
Browse files Browse the repository at this point in the history
  • Loading branch information
szalapski committed Aug 21, 2021
1 parent df8389a commit b3e30eb
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
2 changes: 0 additions & 2 deletions Sz.BlazorRerenderReducers/Client/Pages/MainPanel.razor
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
@code {
[Parameter] public Foo InputFoo { get; set; } = null!;


//protected override string GetDisplayHash() =>
// $"Bar={InputFoo.Bar},Qux={InputFoo.Qux}";
protected override string[] GetDisplayItems()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,19 @@ public abstract class DisplayHashRerenderComponentBase : ComponentBase
protected override bool ShouldRender()
{
if (!EnableRerenderReductionGlobal) return true;

string? displayHash = GetDisplayHash();
Console.WriteLine($"{GetType()} GetDisplayHash='{displayHash ?? "null"}'");

if (displayHash == null)
{
string[]? items = GetDisplayItems();
if (displayHash == null) return true;
if (items == null) return true;
displayHash = string.Join(",", items!);
Console.WriteLine($"{GetType()} GetDisplayItems='{displayHash ?? "null"}'");
}


bool result = PreviousDisplayHash == null || PreviousDisplayHash != displayHash;
PreviousDisplayHash = displayHash;
return result;
Expand Down
2 changes: 2 additions & 0 deletions Sz.BlazorRerenderReducers/Shared/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Suppose that you have a component that is being rerendered undesirably often. F
protected override string GetDisplayHash() => InputFoo.Bar.ToString();
}
```
Alternatively, override GetDisplayItems with several strings that represent, collectively, *all* the displayed state of the component.

Now your component should be rerendered only when the value of GetDisplayHash changes. To see such rerendering logged, you might choose to override AfterRender on your component.

Note that child components will not rerender if the current component doesn't rerender, as the current component will not set any parameters on its children unless it rerenders.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
<TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.1.0</Version>
<Version>0.2.0</Version>
<Authors>Patrick Szalapski</Authors>
<PackageId>Sz.BlazorRerenderReducers</PackageId>
<Company />
<Product />
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Description>Provides classes useful for reducing the frequency of component re-renders of Blazor components to a minimum.</Description>
<Copyright>2021 Patrick Szalapski</Copyright>
<PackageReleaseNotes>0.1.0 Initial release with only hash-based rerendering control</PackageReleaseNotes>
<PackageReleaseNotes>
0.1.0 Initial release with only hash-based rerendering control
0.2.0 Added optional control by array of strings, in addition to string--use either one
</PackageReleaseNotes>
<PackageProjectUrl>https://szblazorrerenderreducers.azurewebsites.net/</PackageProjectUrl>
<RepositoryUrl>https://github.com/szalapski/BlazorRerenderReducers</RepositoryUrl>
<RepositoryType>Git</RepositoryType>
Expand Down

0 comments on commit b3e30eb

Please sign in to comment.