How to create a new React Native project in Visual Studio Code

Cynthia

React Native is a popular framework for building mobile applications using JavaScript and React. Visual Studio Code (VS Code) is a widely used, free, and open-source code editor developed by Microsoft. Combining React Native with VS Code makes for a powerful development environment. In this tutorial, we'll walk you through the steps to create a React Native app in Visual Studio Code.

Prerequisites

Before we begin, make sure you have the following software installed:

  1. Node.js and npm: React Native relies on Node.js and npm for package management. You can download and install them from nodejs.org.

  2. Expo CLI (Optional): Expo is a set of tools that simplifies React Native development. You can install it globally with npm:

    npm install -g expo-cli
    
  3. Visual Studio Code: You can download VS Code from code.visualstudio.com.

  4. Android Studio or Xcode: Depending on whether you want to develop for Android or iOS, you'll need the respective development environment set up. Follow the official documentation for Android Studio and Xcode.

Creating a New React Native Project

Now, let's create a new React Native project using Expo, which simplifies the development setup. If you prefer to use the bare React Native workflow, you can use the react-native-cli instead.

Using Expo

  1. Open your terminal and navigate to the directory where you want to create your project.

  2. Run the following command to create a new Expo-based React Native project:

    expo init MyApp
    

    Replace MyApp with your desired project name.

  3. Follow the prompts to choose a project template. You can start with the “blank” template for a minimal setup.

  4. Once the project is created, navigate to the project folder:

    cd MyApp
    
  5. Start the development server by running:

    expo start
    

    This command will open a new tab in your browser with the Expo DevTools, where you can run your app on a physical device or an emulator/simulator.

Using React Native CLI (Bare Workflow)

If you prefer the bare workflow without Expo, you can create a project with the React Native CLI. Note that this method offers more flexibility but requires additional setup.

  1. Open your terminal and navigate to the directory where you want to create your project.

  2. Run the following command to create a new React Native project:

    npx react-native init MyApp
    

    Replace MyApp with your desired project name.

  3. Once the project is created, navigate to the project folder:

    cd MyApp
    
  4. Start the development server by running:

    npx react-native start
    

    In a separate terminal window, run your app on a physical device or an emulator/simulator by executing:

    • For Android:

      npx react-native run-android
      
    • For iOS:

      npx react-native run-ios
      

Coding Your React Native App

With your project created, you can now start coding your React Native app in Visual Studio Code:

  1. Open VS Code and go to File > Open Folder, then select your project folder (MyApp in this example).

  2. VS Code will load your project. You can explore your codebase in the file explorer on the left side.

  3. Open App.js or the main entry point of your app and start writing your React Native components and logic.

  4. Use the integrated terminal in VS Code to run commands like npm start or expo start to start the development server.

  5. Make changes to your code, and the development server will automatically reload your app, allowing you to see the changes in real-time.

  6. You can also use the built-in debugger in VS Code to debug your React Native code. Set breakpoints, inspect variables, and step through your code for easier debugging.

Running Your App on Devices

To see your app in action on physical devices, you'll need to follow these steps:

  1. Android: Connect an Android device via USB or use an emulator like Android Studio's AVD Manager. Ensure USB debugging is enabled on your device.

  2. iOS: If you're using a Mac, you can run your app on an iOS simulator. If you want to run it on a physical iOS device, connect it to your Mac and select it as the target in Xcode.

  3. Run the appropriate command to start your app on the chosen platform:

    • For Android:

      expo start --android
      

      or

      npx react-native run-android
      
    • For iOS:

      expo start --ios
      

      or

      npx react-native run-ios
      

Your React Native app should now be running on your selected device.

Conclusion

Creating a React Native app in Visual Studio Code is a straightforward process once you have the necessary tools and dependencies in place. VS Code's rich feature set, extensions, and debugging capabilities make it an excellent choice for React Native development. Whether you choose the Expo or the bare workflow, you're now ready to start building cross-platform mobile apps with React Native in Visual Studio Code. Happy coding!