Sign upSign InSign upSign InSascha KlompFollowITNEXT--ListenShareAs a frontend technology, my team was still using Razor, a .NET library to create server-side web pages. Sadly, the speed at which Razor renders its pages and sends them to the client, does not have the performance that is desired nowadays; instead of this archaic server-side request/response model, web pages built in modern frontend frameworks use a more client-side approach for updating content on the page. To select a more modern frontend technology, we compared .NET Blazor, the descendant of Razor Pages, with the popular JavaScript framework React to determine which one would be a better fit for our team. Let’s understand these technologies a little better so a good comparison can be made..NET Blazor is an open-source web development framework which was created by Microsoft in 2018. This framework is used for building interactive client-side web UIs using .NET and C#. It can be considered a descendant of Razor pages, which was previously used with an MVC approach for full stack .NET development.Blazor has been made with modern web development in mind, supporting a couple of different scenarios:Blazor is supported by the most popular modern browsers.The main promise of Blazor is to be able to build full stack web apps without writing a line of JavaScript.In addition, it promises the following things:In its essence, all Blazor apps are component based. This means that elements within the UI are separated into smaller reusable components, such as a modal, form, or table.These components are written in Razor markup, which was already used for Razor pages back in the day. This syntax makes switching between C# and HTML markup possible on the fly. Blazor is not relying on the old-school request and response model, but instead adopts techniques from modern JavaScript UI frameworks by only using components for client-side design and logic implementations.The Blazor components can be reused both in- and outside of their assemblies: within the same project, they can be nested and reused or used in other projects as class libraries or NuGet packages.Even when using the Blazor server-rendering option, Blazor will not send a complete web page over the line every time an action gets triggered. Instead, it will compare its own version of the DOM with the browser’s DOM, distil a difference/diff from it and only sent back this smaller subset through SignalR. This prevents the site from feeling sluggish and slow by having to do a partial/full page reload on every user interaction.A similar technique is used for Blazor WebAssembly (WASM). To recap very briefly, what WASM is: a binary instruction format that can run in a stack-based virtual machine. Essentially, all four modern browsers (Chrome, Firefox, Edge, and Safari) have such a virtual machine integrated into the browser that allows for the compilation of programming languages other than JavaScript. You could compare this a little bit to how Docker works, and it provides you with a memory-safe environment where you can run modules in. This WASM sandbox allows Blazor to access the JavaScript context from within itself, which Blazor uses to construct its own copy of the DOM, does a diff comparison, and corrects the browser DOM accordingly if any changes pop up.Blazor could be an excellent technology for our team because they are already comfortable working with C# and .NET as technologies. Blazor is relatively similar in approach to Razor Pages, although it introduces some new concepts that we would have to familiarize with. It is a relatively mature technology and contains some documentation and community help.React is a full stack JavaScript framework, which is based on Vanilla JavaScript. It was first released by Facebook (now Meta) in 2013 as a free, open-source library. The core of React is very bare bones; it does not by default contain functionalities like routing and data fetching, instead it gives you the flexibility to create your own or implement a third-party solution for them.By using other libraries, it is possible to run React:React promises a component-based system that allows for easy designing and fast loading. It allows the possibility of building full stack applications in React using additional frameworks such as Next.js or Remix.Aside from that it promises:React applications are component based, which means that, just like Blazor, an application consists of smaller components (like a button or form) that can be reusable.Describing these components happens in a special markup language called JSX (JavaScript Syntax Extension), which allows to seemingly mix HTML with React logic. React allows you to manage state for your components. This can be done by hosting your state locally within a component, or in a more complex scenario, through using a global state store like context. This state will be managed by React and it will associate it with the correct node within the UI tree it corresponds to.The way React responds to state changes within a component is to re-render the component and all its descendants. It does this to keep a consistent and up-to-date state of the UI. This UI state is kept in a virtual DOM, a lightweight version of the actual DOM. In addition, React keeps another virtual DOM and uses them both to continuously compare when new changes come in. The result of this difference will be used to update the real DOM. This process makes updating the DOM faster, since the virtual DOMs only contain a blueprint of the DOM tree, but they are never rendered, which saves a lot of time and resources. React calls this technique “Reconciliation”. If you are curious and want to learn more about React’s rendering strategy, check this excellent article.React could be a good fit for the team, because they are already comfortable with using some JavaScript for frontend development. It is the most popular frontend framework and a more popular technology compared to Blazor. The technology is very mature (>10 years old) and has a lot of documentation and community help available.Based on information and experiences with these technologies, we also wanted to get a good practical feel of working with these two technologies, so we ran an experiment of creating two Proof of Concept projects for the same type of application; one in React, the other in Blazor WASM. We divided our development team into equal groups and spent around 20 working days developing the applications.To make a good comparison, we set some common restrictions and requirements. For example, both parties were required to build an application to annotate and watch videos. Specific functionality was listed as required, and as restrictions, we kept the application client-side only.Both teams reported having a pleasant experience getting more used to the technologies. All members of team Blazor could program in C#/.NET already and found the learning curve relatively low. On the other hand, they experienced the more intricate systems like working with the shadow DOM and the changed application lifecycle as a lot more challenging to get comfortable with.On the other hand, the React team was also comfortable with using JavaScript and found the framework very intuitive with a low learning curve. The more complex state techniques like using global stores, were more complex to master.Let’s compare the benefits and disadvantages of both technologies based on our hands-on experiences building the PoCs:Both Blazor and React support the use of components, which makes the application very modular. It provides a lot of abstractions that allow developers to put more focus on the actual design and less on the implementation details. For example, see the React component below that contains all video controls for a specific video:Within a separate screen component, this can be abstracted by importing a component and passing the required parameters. This is especially useful for rendering the same component multiple times with different input:For Blazor this process is similar, where after creating a component it can be used like below:In addition, Blazor also supports data binding and attributes to ease development. This is especially useful for validating forms. By annotating the model with attributes as shown below:By using the bind-Value attribute, it is possible to set the value of the model property when an onchange DOM event is fired. It is also possible to change the event it fires on by using the @bind:event attribute. Doing proper validation like this is also possible in React, but requires installing additional third-party libraries or writing your own custom solution.Both Blazor and React utilize a component lifecycle that is responsible for initially rendering a component, dealing with subsequent re-renders based on state changes and finally destroying the component when the component is no longer needed. In Blazor’s case, this is significantly different from Razor Pages’ lifecycle, and when using it without the appropriate knowledge, it can cause side effects and other problems. Especially when interfacing with JavaScript interop, the Blazor team ran into DOM synchronization issues.Similar challenges are true for React. Whether using the more modern React Hooks or lifecycle methods in a class based component, when used without understanding the component lifecycle, either can cause infinite re-renders, race conditions and other unwanted side effects. For the React team, especially managing state within React hooks posed quite a steep learning curve to the junior in our team. For example, issues would surface when attempts were made to use a state variable together with the method to update itself in one useEffect hook, a very common anti-pattern:When it comes to component lifecycles, it is important to fully understand its intricacies prior to getting started with either technology. Especially when the team is new to this concept, it might take a bit of effort to fully grasp these concepts.Both React and Blazor provide ready-made templates to a certain extent. For Blazor development, when using Visual Studio, it is possible to follow a wizard to get a completely scaffolded template:These options also include using authentication based on individual accounts, or Microsoft Identity Platform. Using connected services within Visual Studio, it is possible to immediately create and set up an Application Registration, which allows for the entire application to work out of the box, including authentication through Azure. Additionally, it is possible to create your own custom Visual Studio template for Blazor. The Blazor team used a standard template including authentication and Progressive Web App (PWA) support to bootstrap their application.React on the other hand, provides templates (from [email protected] up) through running npx create-react-app my-app-name --template cra-template-