Skip to content

Model for referencing a component from another component - declaratively #51340

@htmlsplash

Description

@htmlsplash

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

I would like component B to be able to access component A declaratively.

Ex 1: Consider this markup (using the ref attribute):

ComponentA @ref="compA" 
ComponentB ComponentA="compA"  // where "ComponentA" is a Parameter on Component B

code {
  ComponentA compA;
}

Rule:
As I understand, this will not work because accessing components from life cycle methods except "OnAfterRender" is not possible.

  1. Q:
    How do I achieve something similar what's shown in the example in Blazor? Accessing controls by ID in webforms is a common pattern.

  2. Q:
    Why is the rule (that I stated) not followed consistently about component references.

Ex 2: Consider this markup:

<input @ref="inputField" />

code
{
protected override void OnInitialized()
{
	base.OnInitialized();
      var refToInputField = inputField; // The reference exists, but this will not work for components; Why is there this inconsistency? Is this because I am referencing an HTML element instead of Blazor component?
}
}
  1. Current documentation is either off or not explicit about this:
    Goto:
    https://learn.microsoft.com/en-us/dotnet/architecture/blazor-for-web-forms-developers/components
    Look under heading: Capture component references
    This example is incomplete (in terms of context) and misleading. The OnSomething() is method from where?

Describe the solution you'd like

Questions are stated in the problem.

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions