Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor components vs View Components

I recently upgraded from Asp .NET Core 2.2 to 3.1 and can use Razor Components in razor pages. They look basically like View Components except it uses blazor and everything on the same file. I tried to search but i haven't read anything on documentation that says to use Razor Components instead View Components.

So here is the question...

What is the point of using Razor Components instead View Components in Razor Pages? Is it better? I know View Components are a bit painful to set because you have to set a default view and a code behind file with the logic and you cannot use javascript on it, only on the parent view. Most of my apps are full of View Components and I was wondering what are the advantages of switching to razor components?

like image 368
Jackal Avatar asked Dec 13 '19 19:12

Jackal


People also ask

What is a Razor component?

Blazor apps are built using Razor components, informally known as Blazor components. A component is a self-contained portion of user interface (UI) with processing logic to enable dynamic behavior. Components can be nested, reused, shared among projects, and used in MVC and Razor Pages apps.

What are view components?

A view component consists of two parts, the class (typically derived from ViewComponent ) and the result it returns (typically a view). Like controllers, a view component can be a POCO, but most developers will want to take advantage of the methods and properties available by deriving from ViewComponent .

What is the difference between partial view and view component?

View components are similar to partial views, but they're much more powerful. View components don't use model binding, they depend on the data passed when calling the view component. This article was written using controllers and views, but view components work with Razor Pages.

Can you use Razor components without Blazor?

Razor Components without the BlazorYou can add Razor Components to your Razor Pages, and they can form a little island of client-side interactivity within your otherwise server-side rendered app.


1 Answers

While View Components are more like partial views but asynchronous, DI friendly, and kind of independent. They are mostly for view-only purposes (yeah it's possible to add some update logic via AJAX but it's more like a hack rather than a feature) and are used mostly to re-use common UI units like navigation, login block, sidebar, etc.

Razor Components are sort of "full featured" UI components, which can be easily nested, pass parameters through the components hierarchy, raise and handle UI events, and so on. So you can easily use them to manage your app UI, or even create custom UI component libraries.

like image 179
Dmitry Pavlov Avatar answered Oct 24 '22 08:10

Dmitry Pavlov