Blazor vs React: Pick the Best Stack for your Project
Blazor and React are open-source tools for building interactive single-page applications (SPAs). React is a Javascript library that was launched in 2013 by Facebook to enable developers to build intuitive interfaces and UI components. Blazor on the other hand is a UI framework that was launched in 2018 by Microsoft. Unlike React, Blazor relies on C# and Razor syntax, instead of Javascript, to create reusable and intuitive user interfaces. Until Blazor’s invention, all major frontend frameworks were built on top of JavaScript, so Blazor has created a bit of a splash in the developer community.
That said, even though both tools serve the same function, they’re uniquely suited for different projects. In this article, we’ll compare their features and weigh their pros and cons to help highlight which projects are best suited for each.
Before we get too deep, please hear this quick pitch. If you’re weighing up the technology for a new project and you’re unsure what is the best stack for your situation, please reach out to our team. We’ll either be able to help you or provide free, experience-backed advice!
Since publishing this article we've received a number of questions about how suitable Blazor is as a replacement for React, Vue or Angular in larger SaaS projects. We've added a dedicated question-and-answer section at the end of the article. If you have additional questions, please leave a comment and we'll add the answer to the section so other folks can benefit!
Blazor overview
Blazor is an open-source web development framework developed by Microsoft. It uses C# alongside HTML and CSS, enabling you to build web UI for client-side applications written in. NET. The applications are executed under Web Assembly, meaning they can run on any browser. As such, you don’t require any third-party add-ons and plugins for execution, consequently giving users platform independence.
What makes Blazor stand out as a favorite tool for developers is its interoperability with Javascript. This allows a Blazor app to call Javascript functions from .NET methods and .NET methods from Javascript functions. In addition, unifies the client and server-side using C# as the codebase, consequently empowering developers to share libraries and ultra-modern single-page applications.
Blazor features
Some of the most prominent Blazor features include:
Offers two hosting models
Blazor framework offers two primary hosting models; server-side Blazor and Blazor WebAssembly. The server-side Blazor allows you to host your app on ASP.NET Core where “Razor components'' are executed. Razor components are isolated interface components that can execute logic, enabling dynamic behavior. The frontend sends UI updates, Javascript functional calls, and event handling via a SignalR connection.
Blazor server apps boast complete .NET API compatibility and fast load time. However, they suffer from high latency and scalability challenges.
The Blazor WebAssembly (WASM) model allows you to run your .NET app directly in the browser. To achieve this, the app and its dependencies (including HTM, images, CSS, and .NET runtime) are downloaded to the client’s browser before running. As such, Browser WASM apps don’t require a server connection to run, making the host model ideal for static site hosting. In addition to offloading processing to the browser, Blazor WASM enables clients to run the app offline once the files are downloaded.
Supports HTML and CSS
Although Blazor uses Razor templates to create user interface components, the display is rendered in standard HTML and CSS. As such, you can use any CSS frameworks and preprocessors to style your page. This is great news for teams like ours that have adopted Tailwind CSS.
Additionally, Blazor supports CSS isolation allowing you to style each component individually, thereby eliminating styling conflicts. Blazor also features additional features such as built-in form and input components that help in generating HTML syntax for forms.
Pre-rendering
Most single-page applications experience slow loading time due to the delay between fetching the data from servers to rendering it on the client’s browser. Blazor offers a solution to this problem by offering server-side pre-rendering which ensures that all webpage elements are compiled on the server and static HTML is served to the client. This means that the UI is displayed even before the application can be fully downloaded. As a result, Blazor’s pre-rendering makes SPAs load faster, consequently improving their SEO and customer experience.
Virtualization
Virtualization is a technique for limiting UI rendering to only the items that are currently visible to the user. This concept is useful when dealing with massive amounts of data which can take time to process and display. For example, if your page has over 2000 items, you can use virtualization to render only a small portion of these on the users’ viewport. The rest of the data will be loaded and rendered dynamically as the user scrolls the viewport.
Blazor’s virtualization functionality can be implemented on either Blazor WASM or Blazor server-side applications to improve performance. To enable this functionality, you need to wrap the data using the virtualize HTML tag. Conversely, without virtualization, you’d have to use C# foreach loop to iterate through the items.
Without virtualization:
@foreach (var employee in employees)
{
<tr>
<td>@employee.EmployeeId</td>
<td>@employee.Name</td>
<td>@employee.Role</td>
</tr>
}
Implementing virtualization:
<Virtualize Items="employees" Context="employee">
<tr>
<td>@employee.EmployeeId</td>
<td>@employee.Name</td>
<td>@employee.Role</td>
</tr>
</Virtualize>
Lazy loading
Lazy loading is a design pattern that allows an app to load specific files only as requested by a user. Blazor WASM supports lazy loading which improves the start-up speed of your app. Essentially, this feature defers loading a resource until it's requested by the user, meaning the app doesn’t download all the resources on start-up.
gRPC-web support
gRPC is an open-source remote procedure call framework that facilitates efficient communication between the client and server-side applications. The framework was recently integrated into Blazor alongside a library to help create a gRPC server in ASP.NET and in Blazor client-side.
Finding this writeup useful? You should join our newsletter to receive a monthly summary of our articles and research. We use the newsletter to build a relationship with potential clients. There's no selling or spam.
Blazor pros and cons
Client-side Blazor advantages include:
- Enables you to run .NET applications in a browser
- SPAs can run in an offline state
- Blazor WASM is interoperable with Javascript, allowing developers to use .NET methods and Javascript functions
- Blazor WASM applications can run on most browsers without using additional plug-ins or source compilers
Cons
- Prolonged initial load times especially on devices with a poor connection. This is because it requires the entire .NET runtime to be downloaded
- It has limited debugging capabilities and .NET tools
- Doesn’t work on thin clients
Server-side Blazor advantages include:
- Both server-side Blazor WASM applications are built using C#. This helps speed up the development process and allows developers to focus on more important processes
- Offers fast and efficient build time
- The download size of the app’s components is significantly smaller than the Browser WASM which improves the app’s performance
- Works on all browsers including thin clients and those that don’t support WebAssembly
- Runs on the full .NET Core runtime
Cons
- Reduced scalability. This is because server-side Blazor apps use SignalR connection for event handling. This type of connection has a limit to how many events can be handled from the client-side
- Must be connected to the server at all times and as such it doesn’t offer offline support
- Doesn’t work well in high-latency environments
React overview
React is a Javascript-based library for building UI components. It was developed and launched by Facebook in 2013 and has since then grown to become a popular UI library for building highly intuitive interfaces. It's used by notable companies such as Paypal, DropBox, Twitter, Netflix, and Walmart.
By the time it was released, developers used other frameworks such as Angular and Vue which often required reworking most of the code. React positioned itself as a solution to this problem by enabling developers to create reusable code components thereby speeding up the development process and making it easy to maintain SPAs.
React features
Some of the notable features of React include:
JSX
Javascript syntax extension (JSX) is a combination of HTML and Javascript. It's used in React to easily write HTML and Javascript together, making the code easier to understand and debug. JSX is also faster than the typical Javascript code which helps create a high-performance React app. Here’s a simple JSX code:
const name = ‘JSX sample’
const ele = <h1>This is a {name}</h1
Note that JSX isn’t a valid Javascript code and as such can't be executed directly on a browser. It must be compiled using the Babel compiler to convert it to Javascript code that can be understood by a browser.
Virtual DOM
DOM manipulation is an important aspect of UI web development. It allows developers to modify UI components and perform users’ actions on web elements. However, this process is usually slower in most Javascript frameworks as it involves updating the whole DOM at once, which impairs the performance of a web application. For example, if an app contains a list of twenty items and a user checks off three items only, most frameworks will rebuild the entire list just to update the three items.
To improve performance, React uses virtual DOM which is essentially a representation of the real DOM in memory. This way, whenever an element in the web app is modified, the virtual DOM is updated first and compared with the actual DOM to find any differences in the elements. ReactDOM then updates only the parts of the app that have been modified, while everything else stays the same. This process is called reconciliation.
One-way data binding
React uses unidirectional data flow, which means that the data flows in one direction from parent components to child components. As such, the properties of a child component cannot send data to its parent component, but they can communicate to modify states based on provided inputs. This model helps keep all app activities modular and fast.
Components
React divides the user interface into numerous components. These components have their own set of properties and functions, making them easy to debug. The components can also be reused which helps speed up the development process. Other features of React components include:
- Nesting - meaning a component can contain several other sub-components.
- Render method - this allows you to define how a specific UI component will be rendered in the DOM
- Passing properties - enables a component to receive properties from its parent component.
Extension support
React supports numerous extensions that help in building a full-fledged UI application. They also enable React to support mobile app development and server-side rendering. Some of the extensions include; Flux, Redux, and React Native.
React pros and cons
Advantages of using React include:
- Improves app performance thanks to the virtual DOM capability
- Saves time spent in the development process through reusable components
- Uses less and maintainable code that’s easy to understand and debug
- Improves SEO due to improved performance
- Has a wide range of developer tools
- Underpins Next.js (our favourite frontend framework)
- The library is updated regularly to offer more functionality
- React has a strong community of users that provides resources and solutions on the best ways to use the library
Cons
- Lacks sufficient documentation, especially for new updates and releases
- Due to its fast-paced updates, developers have to constantly invest in learning about new React developments
Comparing Blazor and React for building SPAs
Blazor is a relatively new UI framework that offers an alternative to using Javascript for SPAs. It uses C# as its primary language to build interactive web applications. As such, for businesses that already have a team of .NET developers Blazor is your best choice. It helps eliminate the hustle of hiring Javascript developers to build SPAs since the existing developers can easily build reusable code for both the front and backend.
React is a well-established Javascript-based UI library that has numerous third-party libraries, packages, and extensions. These tools help streamline the development process and optimize React apps. Besides, React helps build high-performance SPAs, making it a better choice than Blazor.
Frequently Asked Questions
Q: Is Blazor faster than React?
No. React apps usually load faster than Blazor, especially when compared to Blazor WASM, which has to download the entire .NET runtime in addition to other app dependencies. However, Blazor server-side apps are slightly faster than React apps.
Q: My team are .net experts and aren't familiar with JavaScript. Is Blazor the right choice for us?
Yes and no. If your team really don't want to use JavaScript or bring in an experienced agency to build your frontend, then yes, Blazor is the right choice. But not because it's better than JavaScript frameworks, simply because it's the best option that isn't JS based. React, Vue and Angular are years ahead of Blazor in terms of maturity, resources, and tooling.
While your team might pick up Blazor quickly, you're ultimately trading off the chance to adopt a better stack.
There are tons of high-quality libraries, packages and tools specifically made to work with JavaScript frameworks that won't work with Blazor. Imagine being in a situation where you find a library that would achieve a complex requirement with 15 minutes of implementation, and then realising you need to spend a whole week to build the same feature because of your tech stack.
Q: Is Blazor mature enough for enterprise development?
It really depends on the complexity of your application. Blazor is still relatively immature, compared with the popular JavaScript frameworks.
There's nothing that would prevent Blazor from building a enterprise application, especially if it's paired with a microservice architecture REST API with asp.net core.
However, as we pointed out in the last answer, Blazor is not as mature as the popular JavaScript frameworks so there will be times when you probably wish you were working with the same stack as everyone else.
Q: Is Blazor replacing Javascript?
No. Javascript is a popular language for building SPAs and has a booming ecosystem of development tools. Its frameworks and libraries, especially React, are well-established and have a proven track record of building robust applications. Blazor on the other hand is yet to mature beyond its experimental stage and win the conviction of Javascript developers.
Conclusion
Overall, when deciding whether to use Blazor or React, you have to consider the trade-offs that come with each tool. Most importantly, it all depends on your team - Blazor works best for a team of .NET developers while React is well-suited for Javascript developers. If you have any questions about Blazor vs React we didn’t answer, please leave them in the comments section below and our team will respond in a day or two.