DEV Community

Renicius Pagotto
Renicius Pagotto

Posted on • Updated on

Azure Static Web Apps with React and Visual Studio Code

Hey everyone, in today's post we are talking about how deploy an React Application to Azure Static Web App using Visual Studio Code.

To understand what is Azure Static Web App, visit Azure Static Web Apps - The Big Picture

Prerequisites

Installing React Framework

Once all the prerequisites are installed on your machine, the next step is to install React, for that run the command below.

npm install -g create-react-app

Creating a ReactJS project

Now that you have React installed, you can create a new react project directory by running the command below

npx create-react-app <project-name>

After creating the project, let's open it in Visual Studio Code. You can open the project using the interface or by typing the command below in your command line tool.

code .

Important: Always access the project root before opening in Visual Studio Code or you can open VS Code first and then open the project root. Always avoid opening the folder of the project you created in the previous folder, as this can lead to errors in the build and consequently in the pipeline.

Image description

The next step is the deployment, so Press F1 and search for Azure Static Web App: Create Static Web Apps and select.

Image description

In the next step, we need to select the Azure Subscription, but if you are not logged into your Azure account with Visual Studio Code, an option to make the connection will be displayed.

Image description

In the next option, we need to define the name to be given to the service in Azure.

Image description

In the next step, it's time to define a name for the GitHub repository

Image description

The next step is to define the region where the resource will be created.

Image description

Now it's time to define the project type. Note that Azure Static Web Apps supports multiple frameworks and since our project is a React project, we should select the React option.

Image description

Here we need to set the path to the root of the React project. By doing this, the executor responsible for deploying the code will be able to run the npm run build command to generate the outputs in the build directory. Let's set / to this option.

Image description

In the last step, we need to define the application build output path and for React projects we need to inform the build folder that it's generate by build command.

Image description

Finally, press enter to start creating the resource on Azure and creating the repository on GitHub.

Image description

Once the run is complete, we have an instance of Azure Static Web Apps provisioned in Azure and ready to use, and the repository on GitHub with our code. At the end, the code deployment pipeline will be executed automatically by GitHub Actions.

Image description

Image description

Image description

Now, let's open the Azure Portal to get the link of our the Web Site in Azure Static Web App resource.

Image description

With the link copied, paste it into your browser to see the result or you can simply click on the link to be redirected automatically.

Image description

Conclusion

Azure Static Web Apps is a powerful feature that makes it easy and fast to build and deliver a modern front-end project.

Did you like this post? Want to talk a little more about it? Leave a comment with your questions and ideas or get in touch through LinkedIn.

Top comments (0)