Razor components vs View Components
Asked Answered
G

2

20

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?

Gaige answered 13/12, 2019 at 19:37 Comment(1)
After assigning bounty, I've found this answer useful: #59262991Kessel
H
24

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.

Heliotropin answered 2/2, 2020 at 20:34 Comment(3)
are those components used only as Blazor?Gilleod
can i use javascript inside View Components ??Seldun
Yes, see #44511834Heliotropin
S
0

As you said above, Razor Components uses one file for everything but View Components is a little boring.

Razor Components look better but have a big problem that you can't use Tag Helpers inside them. So that makes it hard to use Razor Components for forms or other similar parts that uses Tag Helpers.

Sauceda answered 28/2, 2023 at 18:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.