C# > Blazor and Web Development > Blazor Components > Creating a Blazor Component

Component with Parameters: Displaying a Message

This snippet showcases how to create a Blazor component that accepts parameters. This allows for more reusable and configurable components. We'll build a simple message display component.

Component Code (@MessageDisplay.razor)

This code defines a Blazor component named `MessageDisplay`. It includes:

  • `

    @Title

    `: Displays the value of the `Title` parameter as a heading.
  • `

    @Message

    `: Displays the value of the `Message` parameter as a paragraph.
  • `@code { ... }`: This section contains the C# code for the component.
  • `[Parameter] public string Title { get; set; }`: Declares a string property named `Title` and decorates it with the `[Parameter]` attribute. This makes the property a component parameter that can be set from the parent component.
  • `[Parameter] public string Message { get; set; }`: Declares a string property named `Message` and decorates it with the `[Parameter]` attribute.

<h3>@Title</h3>
<p>@Message</p>

@code {
    [Parameter]
    public string Title { get; set; }

    [Parameter]
    public string Message { get; set; }
}

Using the Component in Another Component (e.g., Index.razor)

This code shows how to use the `MessageDisplay` component in another component, such as the `Index` page:

  • ``: An instance of the `MessageDisplay` component is created. The `Title` parameter is set to "Welcome!", and the `Message` parameter is set to "This is a parameterized Blazor component."
  • ``: Another instance of the component is created with different parameters.

@page "/"

<h1>Hello, world!</h1>

<MessageDisplay Title="Welcome!" Message="This is a parameterized Blazor component." />

<MessageDisplay Title="Important Notice" Message="Please update your profile." />

Concepts Behind the Snippet

  • Component Parameters: Parameters allow you to pass data into a component, making it more reusable.
  • [Parameter] Attribute: This attribute identifies a property as a component parameter.
  • Component Composition: Blazor applications are built by composing components together.

Real-Life Use Case

This pattern is incredibly common. Imagine a `ProductCard` component. Parameters could include the product name, price, image URL, and description. This same component can be used to display different product details throughout the application.

Best Practices

  • Use clear and descriptive parameter names.
  • Provide default values for parameters when appropriate.
  • Consider using strongly-typed parameters (e.g., enums or custom classes) instead of strings when possible.
  • When parameter values need to be updated frequently, consider using `EventCallback` for two-way binding or reacting to parameter changes.

Interview Tip

Be prepared to explain how component parameters work and why they are important. Understand the difference between passing data as parameters and using services for data access. Be able to describe scenarios where component parameters are the best choice.

When to Use Them

Use component parameters whenever you need to customize the behavior or appearance of a component based on data from the parent component. This makes the component more flexible and reusable.

Memory Footprint

The memory footprint of a component with parameters is generally small, mainly consisting of the space required to store the parameter values. The impact on performance is usually negligible unless you are passing very large objects as parameters or have an extremely large number of component instances.

Alternatives

Alternatives to using parameters include:

  • Cascading Parameters: Useful for providing data to deeply nested components without explicitly passing it through each level.
  • Services: Suitable for sharing data and logic across multiple components.
  • State Management Libraries (e.g., Fluxor, Redux.NET): Useful for managing complex application state.

Pros

  • Reusability: Components can be easily reused with different data.
  • Flexibility: Parameters allow you to customize the component's behavior.
  • Testability: Components with parameters are easy to test with different input values.

Cons

  • Complexity: Managing a large number of parameters can make a component harder to understand.
  • Type Safety: Incorrect parameter types can lead to runtime errors (using strongly-typed parameters mitigates this).

FAQ

  • What happens if I don't specify a value for a component parameter?

    If you don't specify a value for a component parameter, it will have its default value (e.g., `null` for strings, `0` for integers, `false` for booleans). You can use nullable types or conditional logic to handle cases where a parameter might not be provided.
  • Can I use complex objects as component parameters?

    Yes, you can use complex objects as component parameters. This allows you to pass more structured data to the component. However, be mindful of the potential performance implications of passing large objects.
  • How can I update a parameter value from within the component?

    You cannot directly update a parameter value from within the component if you want to propagate that change to the parent component. You should use an `EventCallback` to notify the parent component that the parameter value has changed. The parent component can then update the parameter value and re-render the child.