|
Hey Reader, Today, I show you how combining Blazor and JavaScript can give you the best of both worlds. Letās dive into how you can easily mix the power of these two technologies in your projects! Watch on YouTube šŗCheck out the full tutorial on YouTube to see everything in action š A Simple Example: Console Logging in BlazorImagine youāre working on a Blazor Server application, and you want to log the current count (of the Counter page) to the console. Your first thought might be to use
This works, but youāll only see the output in the terminal, not the browser console. But what if you want to log it directly in the browser console? This is where JavaScript comes in handy. Injecting JavaScript into BlazorTo use JavaScript, you can inject the When you run this, youāll see the log directly in your browserās console. This is a basic example, but you can use this approach to call any JavaScript function from Blazor. Using Custom JavaScript FilesBut what if you want to use your own JavaScript functions? Just create a new JavaScript file in your projectās
Now, reference this file in your Finally, call this custom function from Blazor: When you click the button, youāll see your custom log message in the console. Logging Complex ObjectsWant to log an entire object instead of just a text or value? You can easily do this with JavaScript too. For example, letās say you have a You can log this object directly: JavaScript will display the object in the console, showing all its properties. Calling Blazor from JavaScriptSometimes, you might want to call a Blazor function from JavaScript. Although itās less common, itās possible! Hereās a quick example: Create a static Blazor method and mark it with the In your JavaScript, call this method: When this JavaScript function runs, it calls the Blazor method and increments the counter. Wrapping UpCombining Blazor with JavaScript opens up a world of possibilities for your web development projects. Whether youāre logging data, handling complex objects, or calling Blazor methods from JavaScript, these techniques can make your applications more powerful and flexible. What's your experience with Blazor and JavaScript? Any real-world examples where you need a specific JavaScript library? Happy coding and have an awesome weekend! Take care, Patrick PS: Need help understanding .NET & Blazor? There are two ways I can help you with:
PPS: Would you like to sponsor this newsletter? Click here. š |
Become a .NET & Blazor expert with weekly tutorials featuring best practices and the latest improvements, right in your inbox.
Hey Reader, .NET 10 is almost here, and it brings a big upgrade for validation in Minimal APIs. In this weekās tutorial, Iāll walk you through how validation works now in .NET 10 (using Entity Framework, Scalar, and Minimal APIs). Before, writing validation logic in each endpoint was a pain. You had to manually check for null values, invalid quantities, or missing fields. But in .NET 10, thatās all built-in and super clean. Watch the full tutorial now: š Happy coding! Take care, Patrick
Hey Reader, If youāve been coding for a while, youāve probably asked yourself this too: āAm I getting better⦠or just older?ā Iāve been writing software for fifteen years now, and that question still sneaks up on me. But looking back, Iāve realized something: real growth in tech isnāt about frameworks or chasing trends. Itās about staying curious, patient, and keeping your spark alive when everything feels heavy. So I made a new video about it, my 15 biggest lessons from 15 years of coding....
Hey Reader, If youāre a Blazor developer, you know that annoying flash when your app loads data during pre-rendering. Itās been around forever. Sure, you could disable pre-rendering, but then you'd hurt performance and SEO. Well⦠with .NET 10, we finally have a real fix: the new Persistent State Attribute. š š„ Watch my full tutorial about it here: In this video, I walk you through: ā What causes the flashing issue ā Why disabling pre-rendering isnāt ideal ā How the new Persistent State...