{"id":11164,"date":"2024-12-29T22:38:10","date_gmt":"2024-12-29T22:38:10","guid":{"rendered":"https:\/\/putridparrot.com\/blog\/?p=11164"},"modified":"2024-12-29T22:38:10","modified_gmt":"2024-12-29T22:38:10","slug":"using-blazor-components-within-react","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/using-blazor-components-within-react\/","title":{"rendered":"Using Blazor components within React"},"content":{"rendered":"<p>One of the many cool things with Blazor is that we can actually use a Blazor component within a React application&#8230;<\/p>\n<ul>\n<li>Create React app using\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nyarn create react-app my-app-js\r\n<\/pre>\n<p>In this example, we&#8217;ll use JS instead of Typescript, but I&#8217;ll show how to use Typescript later\n<\/li>\n<li>Create a Blazor Web Assembly (or Server)\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ndotnet new blazorwasm-empty -o BlazorComponents\r\n<\/pre>\n<\/li>\n<li>Within the Blazor project, add a Blazor component, here&#8217;s a simple starter\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&lt;h3&gt;Welcome to My Component&lt;\/h3&gt;\r\n\r\n&lt;button @onclick=&quot;UpdateCounter&quot;&gt;OK&lt;\/button&gt;\r\n\r\n&lt;div&gt;@_counter&lt;\/div&gt;\r\n\r\n@code {\r\n    int _counter = 0;\r\n\r\n    private void UpdateCounter()\r\n    {\r\n        _counter++;\r\n    }\r\n}\r\n<\/pre>\n<\/li>\n<li>Add the Nuget package <em>Microsoft.AspNetCore.Components.CustomElements<\/em><\/li>\n<li>Within the Program.cs add\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nbuilder.RootComponents.RegisterAsCustomElement&lt;MyComponent&gt;(&quot;my-component&quot;);\r\n<\/pre>\n<p>The <em>my-component<\/em> is the name that gets used within the React app.\n<\/li>\n<li>To test, simple add the component to you Blazor project&#8217;s Index.razor and then run the project<\/li>\n<li>When you&#8217;re happy, run\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ndotnet publish\r\n<\/pre>\n<\/li>\n<li>We now need to copy the <em>_content<\/em> folder from ${project}\\bin\\Release\\net9.0\\publish\\wwwroot to the public folder of the React app<\/li>\n<li>In the index.html file within the public folder of the React application add the following before the &lt;\/body&gt; tag\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&lt;script src=&quot;_framework\/blazor.webassembly.js&quot;&gt;&lt;\/script&gt;\r\n<\/pre>\n<\/li>\n<li>Finally add our component (using the name we gave it earlier) to our React code, so for example my App.tsx looks like this\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&lt;div className=&quot;App&quot;&gt;\r\n  &lt;my-component&gt;&lt;\/my-component&gt;\r\n&lt;\/div&gt;\r\n<\/pre>\n<\/li>\n<\/ul>\n<p>Now if we created our React application using Typescript, which is my usual way of doing this, using<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nyarn create react-app my-app-ts --template typescript\r\n<\/pre>\n<p>and if you follow all the instruction for the React side of things from above, you&#8217;ll find that there&#8217;s an error message displayed because &lt;my-component&gt; as it&#8217;s not an instrinsic JSX type, so the simplest way to handle this is to disable the error using ts-ignore, i.e.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&lt;div className=&quot;App&quot;&gt;\r\n  {\/* @ts-ignore *\/}\r\n  &lt;my-component&gt;&lt;\/my-component&gt;\r\n&lt;\/div&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>One of the many cool things with Blazor is that we can actually use a Blazor component within a React application&#8230; Create React app using yarn create react-app my-app-js In this example, we&#8217;ll use JS instead of Typescript, but I&#8217;ll show how to use Typescript later Create a Blazor Web Assembly (or Server) dotnet new [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[305,243],"tags":[],"class_list":["post-11164","post","type-post","status-publish","format-standard","hentry","category-blazor","category-react"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11164","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=11164"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11164\/revisions"}],"predecessor-version":[{"id":11173,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/11164\/revisions\/11173"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=11164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=11164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=11164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}