Getting Started with React Native for Windows: Complete Setup Guide

Getting Started with React Native for Windows: Complete Setup Guide

If you’re looking to develop a React Native app for Windows, this guide will walk you through the process, from creating your first project to running it successfully. By following these steps, you’ll have your React Native for Windows environment up and running quickly.

How to Set Up the Local SDK Path in React Native

Before getting into project creation, ensure your environment is correctly configured to recognize the Android SDK. Setting up the local SDK path is simple:

Go to your React Native project’s android folder.

Create a file named local.properties.

Add the following line to define the SDK path (replace <your-username> with your Windows username):properties

sdk.dir=C\:\\Users\\<your-username>\\AppData\\Local\\Android\\Sdk

This configuration allows the Android SDK to be recognized and ensures smooth compilation.

Creating a New React Native Project for Windows

Let’s dive into creating and running a new React Native project on Windows.

Step 1: Install All Dependencies

Before starting, ensure you’ve set up your development environment with all necessary tools like Node.js and Android Studio. You can refer to the official React Native Getting Started Guide for details on installing these dependencies.

Step 2: Create a New React Native Project

Run the following command from the directory where you want your new project to be located. This will initialize a fresh React Native project:

npx --yes @react-native-community/cli@next init <projectName> --version "latest"

This command creates a new project with the name <projectName> and ensures you are using the latest React Native version.

Step 3: Navigate Into Your Project Directory

Once your project is created, navigate into the newly generated project folder:

cd <projectName>

Step 4: Add React Native Windows to Your Project

Now that you have a basic React Native project, it’s time to add support for Windows by adding the react-native-windows dependency:

Using Yarn (recommended):

yarn add react-native-windows@^0.75.0

Using NPM:

npm install react-native-windows@^0.75.0

Step 5: Initialize React Native Windows

With the react-native-windows dependency installed, initialize the native Windows code and projects by running:

npx react-native init-windows --overwrite

This command sets up the necessary Windows-specific files in your project.

Note: If you’re working on an existing app with a modified metro.config.js file, make sure to back up your customizations before overwriting it with this command.


Running a React Native Windows App

Now that your project is set up, you can start running it on Windows. Ensure your machine meets the requirements for building a Windows app before proceeding.

Running Without Visual Studio

If you don’t want to use Visual Studio, you can run the app directly using the CLI:

npx react-native run-windows

This will open a Command Prompt window that handles the React packager and your Windows app. The first run may take a while as it builds all the necessary components.

Running with Visual Studio

First, run:

npx react-native autolink-windows 

This command automatically links the Windows-specific dependencies.

Open the solution file in Visual Studio (e.g., windows/AwesomeProject.sln if your project is named AwesomeProject).

Select the Debug configuration and x64 platform from the dropdown menu in Visual Studio.

In the terminal, start the Metro bundler by running:

npm start

Click Run in Visual Studio, or select Debug -> Start without Debugging. You should see your app open, and a new tab in Chrome with http://localhost:8081/debugger-ui/.

Using VS Code

You can also run your React Native Windows app using Visual Studio Code. Here’s how:

  • Install the React Native Tools plugin for VS Code.
  • Create a .vscode/launch.json file in your project root directory and add the following configuration:json
{ "version": "0.2.0", "configurations": [ { "name": "Debug Windows", "cwd": "${workspaceFolder}", "type": "reactnative", "request": "launch", "platform": "windows" } ] }
  • Press F5 or go to the debug menu and select Debug Windows to run the app.

With this guide, you can now set up and run a React Native project on Windows with ease, allowing you to start building cross-platform applications that work on mobile and desktop. Happy coding! 🎉

Leave a Reply

Your email address will not be published. Required fields are marked *