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:
@Message@Title
`: Displays the value of the `Title` parameter as a heading.
<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:
@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
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
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:
Pros
Cons
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.