-
Notifications
You must be signed in to change notification settings - Fork 803
Add React samples #574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add React samples #574
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7ff3ee9
Add DotNetWasmReact and BlazorWebAssemblyReact samples
guardrex 8baff38
Updates
guardrex 4e6b6b1
Updates
guardrex 2e719b1
Update 10.0/BlazorWebAssemblyReact/README.md
guardrex b31cced
Updates
guardrex 3a5dfbb
Updates
guardrex 4ebbd15
Updates
guardrex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
|
||
| # dependencies | ||
| /node_modules | ||
| /.pnp | ||
| .pnp.js | ||
|
|
||
| # testing | ||
| /coverage | ||
|
|
||
| # production | ||
| /build | ||
|
|
||
| # misc | ||
| .DS_Store | ||
| .env.local | ||
| .env.development.local | ||
| .env.test.local | ||
| .env.production.local | ||
|
|
||
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* | ||
|
|
||
| **/bin/** | ||
| **/obj/** | ||
| **/.vs/** | ||
|
|
||
| **/build/** | ||
| **/node_modules/** | ||
| **/public/*.wasm | ||
| **/public/*.js | ||
| **/public/*.dat |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # Blazor WebAssembly in a React app | ||
|
|
||
| This sample shows how to use Blazor WebAssembly integrated into a React app. | ||
|
|
||
| * [Blazor Community Standup - Integrate .NET in JavaScript apps](https://www.youtube.com/watch?v=tAh899Gri4E) | ||
| * [.NET + React demo (`DotNetWasmReact`, .NET 10 or later, `dotnet/blazor-samples` GitHub repository](https://github.com/dotnet/blazor-samples) | ||
guardrex marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * [Host and deploy: JavaScript bundler support](https://learn.microsoft.com/aspnet/core/blazor/host-and-deploy/#javascript-bundler-support) | ||
|
|
||
| ## Project structure | ||
|
|
||
| * `app`: Target React app using rollup to do the JavaScript build. | ||
| * `blazor`: Blazor WebAssembly app containing several components. | ||
|
|
||
| ## Live demo | ||
|
|
||
| https://maraf.github.io/blazor-wasm-react/ | ||
guardrex marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
guardrex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ## Building the source code | ||
|
|
||
| ### .NET | ||
|
|
||
| * Install the [.NET SDK](https://dotnet.microsoft.com/download) (.NET 10 or later). | ||
| * Run `dotnet publish` in the `blazor` folder. | ||
|
|
||
| ### App | ||
|
|
||
| In the `react` folder: | ||
|
|
||
| * Run `npm install`. | ||
| * Run `npm run build`. | ||
|
|
||
| ## Producing bundler-friendly build output | ||
|
|
||
| In JS, file dependencies, such as other JS files or images, are resolved using `import` statements. In browsers, only JS files can be imported using `import` statements. In .NET 10 or later, the .NET SDK can produce build output for JavaScript (JS) bundlers with the MSBuild property `WasmBundlerFriendlyBootConfig` set to `true`. | ||
|
|
||
| Only publishing the app (`dotnet publish` or **Publish** in Visual Studio/Visual Studio Code) copies all files to the output directory. Bundler-friendly output isn't generated when building an app to make incremental builds faster. Making it work for build output should be possible by properly mapping imports to individual locations using a bundler's custom plugin. Microsoft doesn't provide any such plugin at the moment. | ||
|
|
||
| For more information, see [Host and deploy: JavaScript bundler support](https://learn.microsoft.com/aspnet/core/blazor/host-and-deploy/#javascript-bundler-support) in the ASP.NET Core Blazor documentation. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <h1>Counter</h1> | ||
|
|
||
| <p role="status">Current count: @currentCount</p> | ||
|
|
||
| <button class="btn btn-primary" @onclick="IncrementCount">Click me</button> | ||
|
|
||
| @code { | ||
| private int currentCount = 0; | ||
|
|
||
| [Parameter] | ||
| public int Initial { get; set; } = 0; | ||
|
|
||
| protected override void OnParametersSet() => currentCount = Initial; | ||
|
|
||
| private void IncrementCount() => currentCount++; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <h1>Date and Time</h1> | ||
|
|
||
| <p role="status">Current Date and Time: @currentDateTime.ToString("F")</p> | ||
|
|
||
| <button class="btn btn-primary" @onclick="UpdateDateTime">Refresh</button> | ||
|
|
||
| @code { | ||
| private DateTime currentDateTime = DateTime.Now; | ||
|
|
||
| private void UpdateDateTime() | ||
| => currentDateTime = DateTime.Now; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| using Microsoft.AspNetCore.Components.Web; | ||
| using Microsoft.AspNetCore.Components.WebAssembly.Hosting; | ||
| using blazor; | ||
|
|
||
| var builder = WebAssemblyHostBuilder.CreateDefault(args); | ||
| builder.RootComponents.RegisterCustomElement<Counter>("blazor-counter"); | ||
| builder.RootComponents.RegisterCustomElement<DateTimeNow>("blazor-date-time-now"); | ||
|
|
||
| await builder.Build().RunAsync(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| @using System.Net.Http | ||
| @using System.Net.Http.Json | ||
| @using Microsoft.AspNetCore.Components.Forms | ||
| @using Microsoft.AspNetCore.Components.Routing | ||
| @using Microsoft.AspNetCore.Components.Web | ||
| @using Microsoft.AspNetCore.Components.Web.Virtualization | ||
| @using Microsoft.AspNetCore.Components.WebAssembly.Http | ||
| @using Microsoft.JSInterop | ||
| @using blazor |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net10.0</TargetFramework> | ||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| </PropertyGroup> | ||
|
|
||
| <PropertyGroup> | ||
| <WasmBundlerFriendlyBootConfig>true</WasmBundlerFriendlyBootConfig> | ||
| <WasmFingerprintAssets>false</WasmFingerprintAssets> | ||
| <CompressionEnabled>false</CompressionEnabled> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.AspNetCore.Components.CustomElements" Version="10.0.0-preview.7.25351.106" /> | ||
guardrex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="10.0.0-preview.7.25351.106" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import Blazor from './_framework/blazor.webassembly.js'; | ||
|
|
||
| let isBlazorLoaded = false; | ||
|
|
||
| export async function start() { | ||
| if (isBlazorLoaded) { | ||
| return; | ||
| } | ||
|
|
||
| await Blazor.start(); | ||
| isBlazorLoaded = true; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "name": "blazor", | ||
| "type": "module", | ||
| "main": "./main.js", | ||
| "module": "./main.js", | ||
| "exports": { | ||
| ".": { | ||
| "import": "./main.js", | ||
| "require": "./main.js" | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.