The Wayback Machine - https://web.archive.org/web/20211228170930/https://github.com/dotnet/aspnetcore/issues/39217
Skip to content
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

ASP.NET CORE 6 - Blazor - Infer the component type for child components from a method #39217

Open
boukenka opened this issue Dec 28, 2021 · 0 comments

Comments

@boukenka
Copy link

@boukenka boukenka commented Dec 28, 2021

Is your feature request related to a problem?

I am trying to infer the component type from a method. Here are 2 components.

Column.razor

@typeparam TTT

@code {
    [CascadingParameter] public Grid<TTT>? MyGrid { get; set; }

    protected override void OnInitialized()
    {
        MyGrid?.Add(this);
    }
}

Grid.razor

@attribute [CascadingTypeParameter(nameof(TTT))]
@typeparam TTT

<CascadingValue Value="this">
    @Columns
</CascadingValue>

@code {
    [Parameter] public List<TTT>? Items { get; set; }
    [Parameter] public RenderFragment? Columns { get; set; }
    [Parameter] public Func<Task<IEnumerable<TTT>>>? ItemsProvider { get; set; }
    private readonly List<Column<TTT>> MyColumns = new();

    public void Add(Column<TTT> column)
    {
        MyColumns.Add(column);
    }
}

When using these 2 components on the Index page below, I got the following error messages:

RZ10001 The type of component 'Column' cannot be inferred based on the values provided.
Consider specifying the type arguments directly using the following attributes: 'TTT'.
CS0411 The type arguments for method 'TypeInference.CreateColumn_1(RenderTreeBuilder, int)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Index.razor

<Grid ItemsProvider="@Provide">
    <Columns>
        <Column></Column>
    </Columns>
</Grid>

@code {
    List<Test> tests = new();

    async Task<IEnumerable<Test>> Provide()
    {
        await Task.Delay(1);
        return new List<Test>() { new Test() { Name = "Name" } };
    }
}

Describe the solution you'd like

The issue can be fixed by:

A) Define the type explicitly for the Column: <Column TTT="Test">
B) Define the Items parameter for the Grid: <Grid ItemsProvider="@Provide" Items="tests">
C) Use the ItemsProvider delegate defined for the official Virtualize component.

I wish there was another solution that avoided A, B and C. Is this possible to implement it?
It would be more user friendly and easier to implement when proposing a library with components.
I also think that any answer will help people facing the same situation and could be added in the official documentation.
Or maybe I am missing something?

@boukenka boukenka changed the title ASP.NET CORE 6 - Blazor - How can we infer the component type for child components from a method? ASP.NET CORE 6 - Blazor - Infer the component type for child components from a method Dec 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant