Get Started with Windows
This guide will help you get started on setting up your very first React Native for Windows app.
Make sure you have installed all of the development dependencies.
For information around how to set up React Native, see the React Native Getting Started Guide.
Install React Native for Windows
Remember to call react-native init from the place you want your project directory to live.
npx react-native init <projectName> --template react-native@^0.63.2
To create TypeScript template, run
npx react-native init <projectName> --template react-native-template-typescript@6.5.*.
If you've installed react native globally in the past, vianpm install -g react-native, and are having issues with the new instructions, try adding--ignore-existingto yournpxcommand:
npx --ignore-existing react-native init <projectName> --template react-native@^0.63.2instead.
Navigate into this newly created directory
Once your project has been initialized, React Native will have created a new sub directory where all your generated files live.
cd projectName
Install the Windows extension
Lastly, install the React Native for Windows packages.
npx react-native-windows-init --overwrite
The --overwrite flag is a temporary measure that ensures the correct files are copied to
metro.config.jsfor the metro bundler to work with Windows. If you are starting a new app, this should have no impact. If you are adding Windows to your existing app and you have modified themetro.config.jsfile, please back up your changes, run the command and copy over to take effect. We are tracking this issue here.
Here are the options that react-native-windows-init takes:
| Option | Input Type | Description |
|---|---|---|
--help | boolean | Show help. |
--version | string | The version of react-native-windows to use. |
--namespace | string | The native project namespace. |
--verbose | boolean | Enables logging. |
--language | string ["cs", "cpp"] [default: "cpp"] | Which language the app is written in. |
--overwrite | boolean | Overwrite any existing files without prompting. |
--useWinUI3 | boolean | Targets WinUI 3.0 (Preview) instead of UWP XAML. |
--no-telemetry | boolean | Disables sending telemetry that allows analysis of usage and failures of the react-native-windows CLI |
This sends telemetry to Microsoft by default. You can prevent the telemetry from being sent by using the --no-telemetry command line option. See the react-native-windows-init README for more details.
Running a React Native Windows App
Make sure a browser is launched and running before running a React Native Windows app. Also ensure your system meets all the requirements to build a Windows app as well.
Without Using Visual Studio
In your React Native Windows project directory, run:
npx react-native run-windowsA new Command Prompt window will open with the React packager as well as a
react-native-windowsapp. This step may take a while during first run since it involves building the entire project and all dependencies. You can now start developing! :tada:This sends telemetry to Microsoft by default. You can prevent the telemetry from being sent by using the
--no-telemetrycommand line option. See the@react-native-windows/cliREADME for more details.Using Visual Studio
- From the root of the project directory, run the following script which will automatically link your app's dependencies:
npx react-native autolink-windows - Open the solution file in the application folder in Visual Studio (e.g.,
AwesomeProject/windows/AwesomeProject.slnif you usedAwesomeProjectas<projectName>) - Select the
Debugconfiguration and thex64platform from the combo box controls to the left of theRunbutton and underneath theTeamandToolsmenu item. - Run
yarn startfrom your project directory, and wait for the React Native packager to report success. - Click the
Runbutton to the right of the platform combo box control in VS, or select theDebug->Start without Debuggingmenu item. You now see your new app and Chrome should have loadedhttp://localhost:8081/debugger-ui/in a new tab. PressF12orCtrl+Shift+Iin Chrome to open its Developer Tools. :tada:
- From the root of the project directory, run the following script which will automatically link your app's dependencies:
With VS Code
- Open your applications folder in VS Code.
- Install the React Native Tools plugin for VS Code.
- Create a new file in the applications root directory,
.vscode/launch.jsonand paste the following configuration:
{ "version": "0.2.0", "configurations": [ { "name": "Debug Windows", "cwd": "${workspaceFolder}", "type": "reactnative", "request": "launch", "platform": "windows" } ] }- Press
F5or navigate to the debug menu (alternatively pressCtrl+Shift+D) and in the Debug drop-down select "Debug Windows" and press the green arrow to run the application.
Authoring Native Modules
See Native Modules and React Native Windows.
Building a standalone React Native Windows App
Follow these steps to build a version of your app that you can install or publish to the store. This version will package your bundle and assets into the APPX package so you don't need to run Metro.
- Open the solution in Visual Studio
- Select the Release configuration from the Configuration Manager drop-down.
- Build the solution. You can now launch without first launching Metro.
- If you want to build an APPX package to share or publish, use the Project => Publish => Create App Packages... option.
See also this article for additional details: https://techcommunity.microsoft.com/t5/windows-dev-appconsult/getting-started-with-react-native-for-windows/ba-p/912093#