Skip to main content

Command Palette

Search for a command to run...

Creating the .NET React template with TypeScript

Published
4 min read
Creating the .NET React template with TypeScript

When creating a .NET project you have the option of using a React template. Using this project template is extremely useful when trying to get a project up and running quickly. Out of the box, this template allows us to run the npm server on build, proxy requests to the backend, and have our frontend hosted on HTTPS (for development) by using the .NET local certificate.

TLDR: Download the template created in this article from: ReactTypeScript.NET

Creating the project

First thing we need to do is create the project. We are still going to use the React template, but we will modify it later to use TypeScript.

Open Visual Studio and create a new project.

select-template.PNG

Select the .NET Core with React template. Next, give your project a name and select your framework (I will be using .NET 6).

You should now have a .NET project that contains a folder called ClientApp. This is the folder where your React code sits. Since we aren't going to be using the JavaScript code that comes with this template we can go ahead and delete this folder.

folder-structure.PNG

Now that the JavaScript code is gone we can create our TypeScript React project. Open up the command line and navigate to the directory of your .NET project (where the ClientApp folder used to be) and run npx create-react-app client-app --template typescript .

Finally, inside your projects .csproj file update SpaRoot to the folder name of your react-app (I used client-app).

Csproj-File.PNG

Installing Dependencies

There are a few things we need to make sure everything goes smoothly.

Make sure you have a modern version of node (I'm using 16.13.1 for this article). You can run node -v to check the version you are using.

You will also need ts-node installed, if you don't have it already you can run npm install ts-node in your react apps root directory or npm install -g ts-node if you would like to install it globally.

Lastly, navigate to the react app you created before and run npm install @types/node --save-dev this will help you avoid any issues with importing modules.

Configuring the proxy

Now that we have the TypeScript React project created inside our .NET project we want to make it easy to proxy requests to the backend. This is optional, just know that if you don't do this you will have to specify where to send requests (https://localhost:44388/api/request for example).

Open up your launchSettings.json file inside the Properties folder and take note of the application URL and SSL port.

launchsettings.PNG

Next, open your package.json file inside the client-app folder you created and set the proxy property to the URL from the launchSettings file. While we haven't set up HTTPS quite yet we will do that later so we should set it to the HTTPS URL.

proxy-package.PNG

While we're in the package.json we will also add "type": "module" which you can read more about here.

setting-type.PNG

Setting up HTTPS

There are two main files that the .NET React template uses to configure HTTPS, we are going to create those files manually but in TypeScript rather than JavaScript.

Inside the client-app/src folder create two files called aspnetcore-https.ts and aspnetcore-react.ts. Below is what my folder structure looks like after creating those two files.

https-files.PNG

Now we actually have to write some code. Open the aspnetcore-https.ts file you just created and add this code (or you can copy it from here).

aspnetcore-https.PNG

This is going to allow your frontend to use the .NET HTTPS development certificate.

You can now open the aspnetcore-react.ts file and add this code (or copy it from here).

aspnetcore-react.PNG

The aspnetcore-react.ts file sets up the .env.development.local file with environment variables to configure HTTPS.

To finish setting up HTTPS locally we need to have these two scripts run on build. This is pretty simple to do by modifying your package.json script section.

prestart-script.PNG

You can test that everything is working as expected now by running npm run prestart or by running the whole app with IIS.

Troubleshooting

If your app isn't running properly there are a few key areas to check.

  • Verify your node version by running node -v (I'm using 16.13.1)
  • Verify ts-node and @types/node are installed
  • Check that "target": "ES6" and "module": "ESNext" is set in tsconfig.json
  • In the package.json file make sure that "type": "module" is set

Finishing up

At this point, you should have all the features of the .NET React JS template but with TypeScript. Hopefully, one-day Visual Studio will come with an option on top of the React template that lets you select between JavaScript and TypeScript.

You can look at the project created in this article here: ReactTypeScript.NET.

M

Hey,

I had issue with ts-node missing and then following error:

Invalid options object. Dev Server has been initialized using an options object that does not match the API schema. - options.allowedHosts[0] should be a non-empty string

it was simply adding following code to package.json to make the issue fixed:

"options": {
    "allowedHosts": [ "localhost", ".localhost" ],
    "proxy": "your localhost:port address"
  }

I could not add link to proxy as a new user

J

just to clarify, it should show the weather app right?
I get the edit/App.tsx react page

M

followed the steps but 2 tabs are opened,one is React app and one shows: Launching the SPA proxy... 'This page will automatically redirect to localhost:44483 when the SPA proxy is ready.' and keeps loading... then a cmd window shows: "Something is already running on port 3000.Would you like to run the app on another port instead? » (Y/n)"

D

See the above comment, it worked for me.

D

Thank you for interesting article. There is a problem in the code. When spa proxy starts and starts front, then spa proxy does not stop work and spawns a lot of front.

D

Found a solution. The env.development file is missing the content PORT=your port HTTPS=true

D

Dmitry Antipov This worked for me. However, it brings up two web pages. Doesn't really matter, but it's not ideal.

D

Drew Summy

Create .env file in client-app folder and add the following: BROWSER=none

The second web page will never open again

H
Hamed4y ago

Thank you for your great article. I am using Ubuntu 18.0.4. In my case https in dotnet react works fine but with react typescript the registering certificate doesn't work for me and I face with this error

There was an error saving the HTTPS developer certificate to the current user personal certificate store

I tried many solutions through the web but I found no answer and just disabled the https and prebuild command. Do you happen to know how it can be solved?