Introducing Blazor Components [Pt 3] | Front-end Web Development with .NET for Beginners

dotnet
25 Jan 202412:32

Summary

TLDRIn this informative video, Daniel Roth from the Blazer team guides viewers through the fundamentals of Blazer components for building interactive web UIs. He explains how components are reusable pieces of web UI with encapsulated rendering logic and event handling. The tutorial covers defining components with Razor syntax, using built-in components like 'Page Title', and demonstrates component reusability and dynamic rendering with examples like the 'Counter' and 'Weather' components. It also explores passing parameters to components, showcasing the powerful combination of C# and HTML in creating dynamic web applications.

Takeaways

  • 😀 Blazer web apps are composed of reusable components, each encapsulating its own rendering logic and UI event handling.
  • 📝 Components are defined using Razor syntax, which combines HTML with C#, and are stored in .razor files.
  • 📂 The 'Components' folder in a Blazer project contains all the .razor files that define the application's components.
  • 📍 The '@page' directive assigns a URL route to a component, determining its address within the web app.
  • 🏷️ The '<PageTitle>' tag is a built-in Blazer component that sets the title of the web page.
  • 🔄 Components can include C# logic, allowing dynamic rendering based on code execution, such as updating a count on a button click.
  • 📝 The 'onclick' handler in components can be linked to a C# method, demonstrating the integration of C# with UI elements.
  • 🔧 C# code within components can be separated into a 'code-behind' file for cleaner organization, if preferred.
  • 🔄 Components can conditionally render HTML based on C# logic, such as displaying a message when a count exceeds a certain value.
  • 🔁 Loops and conditional statements in C# can be used within components to render lists or other repetitive elements.
  • 🔧 Components can initialize state in the 'OnInitializedAsync' method, simulating asynchronous operations like API calls.
  • 🔧 Parameters can be passed to components as C# properties, allowing customization of their behavior, such as the increment value in a counter.

Q & A

  • What is the main topic of the video?

    -The main topic of the video is about components in Blazer and how to use Blazer components to build rich, interactive web UI.

  • What is a component in the context of Blazer web apps?

    -A component in Blazer web apps is a reusable piece of web UI that encapsulates the rendering logic for the component and how it handles UI events.

  • How are components defined in Blazer?

    -Components in Blazer are defined using Razor syntax, which is a combination of HTML and C#.

  • What is the purpose of the '@page' directive in a Razor file?

    -The '@page' directive assigns an address or location for the component, also known as a route, which determines where the component will be rendered in the web app.

  • What is the 'Page Title' component used for in Blazer?

    -The 'Page Title' component is a built-in component in the Blazer framework that allows specifying the title for the web page.

  • How can components be reused in Blazer?

    -Components can be reused in Blazer by using them as custom HTML tags on the page, allowing for the same component to be used in multiple places within the web application.

  • How does the counter component in Blazer work?

    -The counter component in Blazer works by using a C# method that increments the value of the current count each time the button is clicked, causing the component to re-render and display the updated count.

  • What is the purpose of the 'code-behind' file in Blazer components?

    -The 'code-behind' file in Blazer components is used to separate the C# code from the Razor markup, making it easier to manage and organize the code for complex components.

  • How can you pass parameters to components in Blazer?

    -Parameters can be passed to components in Blazer by defining a C# property with the 'Parameter' attribute, which allows the property to be set from outside the component using an attribute.

  • What is the special method called on a Blazer component that gets invoked after the component is created?

    -The special method called on a Blazer component after it is created is 'OnInitializedAsync', which is used to initialize the component's state.

  • How does the weather component in the video simulate loading data?

    -The weather component simulates loading data by using 'Task.Delay' to mimic an API call, after which it generates random weather forecast data to display in a table.

Outlines

00:00

🛠️ Introduction to Blazor Components

Daniel Roth introduces Blazor components, explaining that they are reusable pieces of web UI in Blazor web apps. These components encapsulate rendering logic and handle UI events. Components are defined using Razor syntax, a combination of HTML and C#. Daniel demonstrates how to locate Razor files in a Blazor web app project and provides an example with the home component, showing how changing the HTML content updates the page. He also explains the 'page' directive for routing and the 'page title' component for setting the page's title.

05:02

🔄 Reusing Components in Blazor

Daniel illustrates how Blazor components can be reused like custom HTML tags. He shows examples with the 'counter.razor' and 'weather.razor' components, demonstrating how they can be added to the homepage by using their respective tags. This allows the counter and weather forecast components to be rendered on the homepage in addition to their original routes, emphasizing the flexibility and reusability of Blazor components.

10:02

🔍 In-Depth Look at a Counter Component

The video dives deeper into the 'counter.razor' component, explaining its structure and functionality. Daniel points out how Blazor components can contain C# logic alongside HTML markup. He demonstrates how the 'currentCount' variable is used to render the count and how a button's onclick event is handled by a C# method that increments the count. He also shows how to refactor the code into a separate code-behind file, keeping the Razor file clean. Conditional rendering and control flow logic within components are also discussed, using examples of if statements and loops to dynamically render content.

Mindmap

Keywords

💡Blazer components

Blazer components refer to the reusable pieces of web UI that encapsulate rendering logic and UI event handling within the Blazer framework. They are the building blocks of a Blazer web application, allowing developers to create interactive and dynamic web pages. In the video, Daniel Roth demonstrates how to use these components to build a rich, interactive web UI, showing examples like the 'counter' and 'weather' components.

💡Razor syntax

Razor syntax is a combination of HTML and C# used to define Blazer components. It allows developers to embed server-side code within HTML markup, making it a powerful tool for creating dynamic web content. The video script mentions using Razor syntax to define components in '.razor' files, which are then used to render web pages with interactive elements.

💡Component routing

Component routing is the process of assigning a URL address or location to a Blazer component, allowing it to be accessed via a browser. The '@page' directive in Razor syntax is used for this purpose, as demonstrated when the 'home.razor' file is shown with a route that renders the home component when the root of the Blazer web app is accessed.

💡Page title component

The page title component is a built-in feature of the Blazer framework that allows developers to specify the title of a web page. It is used within the Razor syntax to set the title displayed in the browser tab. In the script, the 'page title' is used to change the title from 'home' to 'homepage', showcasing its functionality.

💡Component reusability

Component reusability is the ability to use a defined component in multiple places within a web application. This concept is highlighted in the script when the 'counter' component is added to the homepage in addition to its own route, demonstrating how components can be embedded within other components to create a cohesive user interface.

💡C# logic

C# logic refers to the server-side code written in the C# programming language that can be included within Blazer components to handle logic and state management. The script explains how C# methods can be used within components, such as the 'onclick' handler in the 'counter' component that increments the count and triggers a re-render of the component.

💡Code-behind file

A code-behind file is a separate C# file that contains the logic for a Blazer component, allowing for a cleaner separation between the UI markup and the server-side code. The script describes how to extract C# code blocks from a Razor file into a '.cs' file, creating a 'counter.razor.cs' file that contains a partial class for the component.

💡Conditional rendering

Conditional rendering is the process of dynamically displaying or hiding UI elements based on certain conditions. The script demonstrates this with an 'if' statement in the 'counter.razor' file, which checks the value of 'current count' and conditionally displays a 'P' tag with the message 'you win' when the count exceeds three.

💡For loop

A for loop is a control flow statement used to repeat a block of code a certain number of times. In the context of the video, a for loop is used within the 'counter' component to render a list of 'clicked' items based on the count, showing how loops can be utilized in Razor syntax to generate dynamic content.

💡Component parameters

Component parameters are properties defined in a Blazer component that can be passed as arguments when the component is used. The script explains how to define a parameter with the 'parameter' attribute, as seen with the 'increment amount' property in the 'counter' component, allowing the increment value to be customized when the component is instantiated.

💡Inline code blocks

Inline code blocks are used in Razor syntax to execute C# code within the rendering logic of a component. The script mentions defining a new variable using an inline code block to set the 'increment amount' dynamically, demonstrating the flexibility of mixing C# expressions with HTML markup in Blazer components.

Highlights

Introduction to Blazer components and their role in building interactive web UI.

Components in Blazer encapsulate rendering logic and UI event handling.

Razor syntax is used to define components, combining HTML with C#.

Components are defined in .razor files within the components folder.

The @page directive assigns a route to a component in a Blazer web app.

The <PageTitle> component allows specifying the title for a page.

Demonstration of how to include and reuse components within other components.

Counter.razor example shows a component with a route and rendering logic.

Using C# within components for dynamic rendering and event handling.

Separating C# code from markup using the @code directive.

Razor files are essentially C# classes with HTML rendering logic.

Adding control flow logic like conditionals and loops in components.

Weather forecast component example with dynamic data rendering.

Using the OnInitAsync method for component state initialization.

Passing parameters to components for dynamic behavior.

Using C# expressions for component parameters to control behavior.

Combining C# and HTML for powerful dynamic rendering in Blazer components.

Transcripts

play00:01

[Music]

play00:09

hi everyone I'm Daniel Roth from the

play00:11

Blazer team in this video we're going to

play00:14

learn all about components and how you

play00:16

can use Blazer components to build Rich

play00:18

interactive web UI Blazer web apps are

play00:22

made up of components a component is a

play00:25

reusable piece of web UI it encapsulates

play00:29

the rendering logic for the component

play00:31

and also how it handles UI events you

play00:34

define components in Blazer using razor

play00:37

syntax which is a combination of HTML

play00:41

and

play00:42

C razor components are defined in Razer

play00:45

files and let's take a look at our

play00:47

project to see where those are at okay

play00:49

so here's our Blazer web app and we can

play00:51

see that in this components folder right

play00:54

here are all of our razor files that

play00:57

Define the the Blazer components for our

play01:00

web application counter home weather and

play01:02

many others here's the home. Riser file

play01:06

which defines the homepage for our

play01:09

Blazer web app you remember before that

play01:11

it had some HTML content and we could

play01:13

change that HTML content we just save

play01:16

that and that content then updated on

play01:19

the page great so lots of HTML here that

play01:22

makes sense but we've got some other

play01:24

stuff too so up at the top we can see we

play01:27

have this at page directive what's that

play01:30

doing well this is how you can um assign

play01:33

an address or a location for your

play01:35

component it's actually we call it a

play01:37

route you're adding a route for this

play01:39

component and that's why when we browse

play01:41

to the root of this Blazer web app we

play01:44

see the home component render that's why

play01:47

we get that that content there below we

play01:50

also have this page title tag and it

play01:53

looks kind of like an HTML tag but it's

play01:55

a different color and it's using capital

play01:57

letters what is that well page page

play02:00

title is also a component it's a builtin

play02:03

component in the Blazer framework that

play02:05

lets us specify the title for the page

play02:09

so that's why up above here in the tab

play02:10

we see that it says home that's coming

play02:12

from this page title component if we

play02:15

change that to something else like I

play02:16

don't know homepage and save that that

play02:19

should then update and there it goes so

play02:22

now we have homepage in our as our uh

play02:24

the title for this page cool so you can

play02:27

use components from other components

play02:29

using this HTML style syntax all right

play02:33

let's look at some other examples well

play02:35

we have multiple other components in

play02:36

this app like we have counter. Riser

play02:39

counter. Riser is what's you being used

play02:41

to define that counter page on the the

play02:43

Blazer web application if we look at it

play02:46

we can see again it has a a route so

play02:48

that's why when we're looking at the

play02:50

Blazer web app and we browse to slash

play02:53

counter that we end up on we see this

play02:56

component rendered all right so that's

play02:58

cool but if we go back back to home

play03:01

because counter is just another

play03:02

component we should be able to let's do

play03:04

this side by side I'm going to bring up

play03:05

both apps together there's the

play03:08

homepage okay so if we do uh uh write a

play03:12

counter tag and you know the name

play03:14

matching the the name of the razor file

play03:16

and let's go ahead and save that we can

play03:18

see now we have a counter component that

play03:20

shows up on the homepage if we click the

play03:22

button we can see that the count goes up

play03:25

and it's in addition to the counter

play03:27

that's already at the slash counter

play03:29

route that counter still works but now

play03:31

we have another counter on the homepage

play03:34

uh what else could we do well we also

play03:35

have a a weather component you know

play03:37

weather. Riser that's what's defining

play03:40

this weather uh forecast page over here

play03:42

on our uh on our web application so we

play03:45

click on that we can see there's that

play03:47

table of randomly generated weather data

play03:50

uh can we add that to our homepage uh

play03:52

sure let's go to the homepage here and

play03:53

then add another tag called slwe and

play03:56

we'll save that into the application and

play03:58

now we've got a a weather forecast

play04:00

showing up on the homepage so that's how

play04:01

easy it is to reuse Blazer components

play04:04

you just use them as like custom hmill

play04:07

tags on your page all right let's take a

play04:10

deeper look at counter. raiser and see

play04:12

what we have going on here Blazer

play04:14

components can contain uh C logic in

play04:18

addition to just you know HTML markup on

play04:21

this counter page we have a couple

play04:24

things here we've got uh this P tag and

play04:26

the P tag is being used to render the

play04:28

value of the the current count and we're

play04:30

using this little at character to say

play04:33

that we'd like to render the value of

play04:34

this C variable current count below we

play04:38

have a a button there's a button right

play04:40

there and this button has an onclick

play04:43

Handler but instead of pointing to like

play04:45

some some JavaScript this on click

play04:47

Handler is actually running a c method

play04:50

which is defined below so the C method

play04:52

when it runs you can see that it's

play04:54

incrementing the value of the current

play04:55

count field which is just defined up

play04:57

above and then the component re renders

play05:00

and it R renders here in the P tag and

play05:01

then you see the updated value so that's

play05:04

that's how this counter is actually

play05:05

working every time we click that button

play05:07

we're running our our C method so you

play05:09

can run C code from your uh your your

play05:12

Blazer components using razor syntax um

play05:15

now some people don't like having you

play05:18

know a lot of C code sitting alongside

play05:21

their their markup um that's okay uh you

play05:23

can actually move this code block into a

play05:26

separate file if you'd like if we

play05:28

actually click on this code code razor

play05:30

directive there's this little light bulb

play05:31

that shows up and there's this code

play05:33

action that will actually extract that

play05:35

code uh code block and put it in this uh

play05:38

code behind file over here so now we

play05:40

have a counter. razor. CS it's just a C

play05:43

file and we can see that this has

play05:45

generated for us a counter class and

play05:48

it's a partial class now what the razor

play05:51

compiler will do when this app is built

play05:53

is it we'll take this counter partial

play05:55

class and combine it with the C class

play05:58

that gets generated from our counter.

play06:00

Riser file when you think about a a

play06:02

Razer file you can really think about it

play06:04

as just a C class it's a convenient way

play06:08

to write a C class that captures some

play06:10

HTML rendering logic in addition to some

play06:13

some some other code all right so that's

play06:15

what's happening there so that's a

play06:16

pattern that you can use I actually

play06:18

prefer to have all the code in in one

play06:20

file it's all part of the you know the

play06:22

UI layer for my application so I prefer

play06:24

this pattern but feel free to use

play06:26

whatever makes you happy okay so we def

play06:29

find members on our on our component

play06:32

using razor syntax using C what else can

play06:35

we do well we could also add some like

play06:37

control flow logic like let's add an an

play06:40

if statement here like a conditional so

play06:42

if the current count is greater than

play06:44

three let's display some HTML and Razer

play06:48

is really smart about being able to

play06:49

transition back and forth between C and

play06:52

HTML we're just going to put a P tag

play06:53

right in here and we'll render um I

play06:56

don't know you win I can say that's the

play06:58

the magic number number where you uh you

play07:00

win the prize for clicking at least

play07:01

three times or three times or more all

play07:03

right so let's go back to our running

play07:06

application okay so here we're on the

play07:08

the counter page if I click the button

play07:09

one two three and then the fourth time

play07:11

now it's displaying you in at the bottom

play07:14

so we're conditionally rendering some

play07:15

markup uh based on some C logic uh we

play07:19

can do other things like we could have a

play07:20

loop like let's do a for Loop right here

play07:22

you know let's add a little I variable

play07:24

that will initialize a zero and we'll

play07:26

check for when I is less than whatever

play07:29

the current count is and just increment

play07:30

I and then inside this Loop um actually

play07:34

what we'll do is we'll make a little uh

play07:36

unordered list right here and wrap that

play07:39

around our for Loop and then we'll use

play07:41

the for Loop inside to render a bunch of

play07:44

list items um we'll just render you know

play07:46

clicked so we should have the word

play07:48

clicked rendered for however many times

play07:50

we've uh We've incremented the count so

play07:52

let's go ahead and save that and go back

play07:54

to our running app yeah so the current

play07:57

count is four and we you can see we

play07:58

looped uh four times and rendered

play08:01

clicked for every time we ran that that

play08:02

Loop if I click again I get another

play08:04

clicked and so on and so forth okay so

play08:06

that's how you can add some uh you know

play08:08

control flow logic using C to to your

play08:11

Blazer components nice powerful

play08:13

combination of HTML and C in the weather

play08:17

page you can actually see that that's

play08:19

how the weather page is working like in

play08:21

the weather page we have an if statement

play08:23

and we're checking to see do we have the

play08:25

weather forecast data yet have we

play08:27

successfully loaded it uh if not then it

play08:30

starts out by displaying a placeholder

play08:33

and then if we do have the weather

play08:34

forecast data you can see below that

play08:36

we're rendering this nice table with all

play08:38

the the weather details and there's

play08:41

another loop in here that's looping over

play08:43

each weather forecast and rendering the

play08:45

the table rows that make up that table

play08:47

just rendering each value from our well

play08:50

rendering each value from our forecast

play08:53

uh uh data objects down below you can

play08:56

see where we're initializing the weather

play08:58

forecast data in this uninitialized

play09:00

async method so this is a special method

play09:03

on a Blazer component that gets invoked

play09:06

uh right after the component is created

play09:07

so that you can initialize some of its

play09:09

state and here you can see that we're

play09:11

first uh you know just running a little

play09:13

task dot delay to simulate as if we're

play09:15

going off and doing an API call and then

play09:18

after that completes we just generate a

play09:20

bunch of uh weather forecast data

play09:22

randomly so don't don't use this for

play09:23

your next uh you know vacation trip

play09:25

planning this is just random weather

play09:27

data okay uh so once we have the the

play09:30

forecast data then the table renders and

play09:31

we can see that on the the weather page

play09:33

like if we watch the weather uh table

play09:35

render let's refresh it one more time

play09:37

you see initially there I'm pausing it

play09:39

there's this loading dot dot dot and

play09:41

then if we let it continue about half a

play09:43

second later the weather data shows up

play09:45

all right cool we can also pass

play09:48

parameters to components like flow data

play09:51

into your components uh for example

play09:54

maybe on this counter component instead

play09:56

of just incrementing by one we want to

play09:58

be able to spe specify how much the

play10:00

component uh increments byy with every

play10:02

click so to do that we just Define a

play10:04

component parameter and a component

play10:06

parameter is just a c property so I'm

play10:09

going to add a a property here called

play10:12

increment

play10:13

amount amount we'll spell it right get

play10:17

set okay and we'll initialize this to be

play10:20

one by default okay and I want this to

play10:23

be the value that we increment by so a

play10:25

current count instead of just always

play10:27

doing plus plus we'll increment by

play10:29

whatever the value is of increment

play10:31

amount now to make this par uh property

play10:33

an actual parameter I need to add the

play10:36

parameter attribute okay so now it's a

play10:39

proper component parameter what does

play10:40

that mean like how do I use it well if

play10:43

we go to the home component now where we

play10:45

have an instance of the counter

play10:47

component if I start typing increment

play10:49

amount you can see I get intellisense

play10:51

and we can specify the value for that

play10:53

parameter using an attribute like let's

play10:55

say we want it to increment by by two

play10:57

instead of by one so if we go to the the

play10:59

homepage now okay so now if we click on

play11:01

the click me button yeah instead of

play11:03

incrementing by one it's now

play11:04

incrementing by two and all my other

play11:06

logic that I added is still still

play11:07

working if we go to the counter at the

play11:10

SL counter route and I click the button

play11:12

it's still incrementing by one because

play11:13

we didn't specify that parameter so it's

play11:16

just using the the default value and

play11:18

it's important to note that this this

play11:20

value that we're specifying for that

play11:22

parameter it's a full C expression like

play11:25

if I want to put like this should be 1+

play11:27

one you know or Let's do let's do

play11:28

something different OnePlus OnePlus 2

play11:31

and we save that and go back to our app

play11:34

and now if we click the counter on the

play11:36

the homepage you know click now it's

play11:38

incrementing by three so it actually

play11:39

evaluated that that c expression for us

play11:42

if I uh even Define a variable like

play11:44

let's add a little inline code block

play11:47

this is how you do inline code blocks in

play11:48

in in Razer it's like code that's

play11:50

running in line with the rest of the

play11:52

rendering logic we could Define a new

play11:54

variable here let's say this is going to

play11:56

be 1 plus well we'll just do one plus

play11:59

one again and then instead of having

play12:01

this expression we'll actually use that

play12:03

uh amount variable we get intellisense

play12:05

awesome we'll save that and now if we go

play12:08

back to the app and click we can see

play12:10

it's back to incrementing by by two so

play12:13

powerful combination of c and HTML uh

play12:16

we've just seen how you can build Blazer

play12:19

components using razor syntax uh and

play12:22

have components that have Dynamic

play12:23

rendering

play12:27

logic

Rate This

5.0 / 5 (0 votes)

Related Tags
Blazer ComponentsWeb UIRazor SyntaxInteractive DesignWeb DevelopmentComponent ReusabilityHTML IntegrationC# LogicUI EventsEducational Video