Blazor Framework using C#
React is a powerful javascript library which helps us to build responsive, interactive user interfaces for real world or intractive web-based applications. React is purely on the component based framework of web app and doesn't tackle other concerns.If we need to fetch data from a database, you can make API requests from your application to a server.
Blazor is a framework which also useful to build client-side web applications that run in the browser, but using C# instead of JavaScript. Here we can build web application as a series of components, using the Razor, with UI logic written in C#. Blazor app can be published as simple as any other .NET web application.
When a user accesses your Blazor Wasm application, a Blazor JavaScript file takes over which downloads the .NET runtime, your application and its dependencies, before running your app using WebAssembly. Blazor then takes care of updating the DOM, rendering elements and forwarding events (such as button clicks) to your application code.
Commands for creating React App Blazor apps are similar:
React:
npx create-react-app my-app
cd my-app
npm start
Blazor:
dotnet new blazorwasm
cd blazorwasm
dotnet run
React Function Component: function FuncComponent(){ const [name, setName] = useState(); const [address, setAddress] = useState(); function handleNameChange(event){ setName(event.target.value); } function handleCommentsChange(event){ setAddress(event.target.value); } function handleFormSubmit(event){ // submit data to your server alert(`${name}: ${comments}`) event.preventDefault(); } } return ( <form onSubmit={handleFormSubmit}> <label> Name: <input type="text" value={name} onChange={handleNameChange} /> </label> <label> Address: <input type="text" value={address} onChange={handleAddressChange} /> </label> <input type="submit" value="Submit"/> </form> )
Blazor Companent:
@inject HttpClient Http
@using System.ComponentModel.DataAnnotations
<AddForm Model="FormModel" OnValidSubmit="HandleValidSubmit">
<DataAnnotationsValidator />
<label for="name">
Name:
<InputText id="name" @bind-Value="FormModel.Name"/>
</label>
<label for="slug">
Thoughts?:
<InputText id="address" @bind-Value="FormModel.Address"/>
</label>
<input type="submit" value="Submit"/>
<ValidationSummary />
</AddForm>
@code {
protected ContactUsModel FormModel { get; set; } = new ContactUsModel();
async Task HandleValidSubmit()
{
await Http.PostAsJsonAsync("api/AddPerson", FormModel);
// post to your API
}
protected class ContactUsModel
{
[Required]
public string Name { get; set; }
public string Address { get; set; }
}
}
Blazor offers a few advantages, especially if you're coming from a .NET background. You can bring your existing C# skills, experience and knowledge to the modern web application party. You can stick to the ecosystem you already know (NuGet, the dotnet
tooling, Visual Studio or VS Code and IIS on Windows).
Shared view models between client and backend API. This is a big deal and makes it much easier to implement any web application. Routing, form handling and validations can be possible with one framework.
Note : Learning one language is enough to achive this, no need to learn javascript and C# at a time.
Comments
Post a Comment