{"id":22589,"date":"2016-12-15T11:48:26","date_gmt":"2016-12-15T11:48:26","guid":{"rendered":"https:\/\/blog.jetbrains.com\/webstorm\/?p=8684"},"modified":"2021-06-11T12:01:35","modified_gmt":"2021-06-11T11:01:35","slug":"developing-mobile-apps-with-react-native-in-webstorm","status":"publish","type":"webstorm","link":"https:\/\/blog.jetbrains.com\/zh-hans\/webstorm\/2016\/12\/developing-mobile-apps-with-react-native-in-webstorm","title":{"rendered":"Developing Mobile Apps With React Native in WebStorm"},"content":{"rendered":"<p><strong><a href=\"https:\/\/facebook.github.io\/react-native\/\" target=\"_blank\" rel=\"noopener\">React Native<\/a><\/strong> allows you to develop native mobile apps for iOS and Android using JavaScript and React. It\u2019s created by Facebook and used for such well-known apps as Instagram, Airbnb, and now JetBrains\u2019 own <a href=\"https:\/\/blog.jetbrains.com\/zh-hans\/youtrack\/2016\/11\/introducing-youtrack-mobile\">YouTrack mobile app<\/a> (which is <a href=\"https:\/\/github.com\/JetBrains\/youtrack-mobile\" target=\"_blank\" rel=\"noopener\">open-source<\/a>, by the way).<\/p>\n<p>With WebStorm you can now develop mobile apps with React Native getting all the benefits of WebStorm goodies like code completion for React, Flow support, and a built-in debugger. We believe WebStorm can be a great and powerful tool for development with React Native!<\/p>\n<p>Let\u2019s see how we can set up a workflow for development with React Native in WebStorm.<\/p>\n<h2 id=\"install-react-native\">Install React Native CLI<\/h2>\n<p>Make sure you have a React Native CLI installed on your machine. To install it, run the following command in the Terminal: <code>npm install -g react-native-cli<\/code>.<\/p>\n<p>The list of tools you need to install to get started with React Native depends on your OS and the mobile platform you\u2019re going to target your app at. Check out <a href=\"https:\/\/facebook.github.io\/react-native\/docs\/getting-started.html\" target=\"_blank\" rel=\"noopener\">React Native&#8217;s Getting Started guide<\/a> for detailed installation instructions.<\/p>\n<h2 id=\"create-new-react-native-project\">Creating a new project<\/h2>\n<p>Now you can create a new React Native project right from the IDE Welcome screen: click <em>Create new project<\/em>, select React Native from the list on the left, enter a project name and click OK.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-8686\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2016\/12\/webstorm-new-react-native-project.png\" alt=\"new-react-native-project\" width=\"700\"><br \/>\n<!--more--><\/p>\n<p>WebStorm will run a project generator and install all the required dependencies.<\/p>\n<p>Of course you can also open an existing project or check one out from version control.<\/p>\n<p>We recommend that you exclude <em>android<\/em> and <em>ios<\/em> folders from the project. To do that, right-click on a folder in the Project view and select <em>Mark as Excluded<\/em>.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-8687\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2016\/12\/webstorm-mark-as-excluded.png\" alt=\"mark-as-excluded\" width=\"700\"><\/p>\n<h2 id=\"running-react-native-app\">Running and debugging the app<\/h2>\n<p>Now that we have our app\u2019s code in the IDE, let\u2019s run it.<\/p>\n<p><strong>UPD:<\/strong>&nbsp;In WebStorm 2018.1, we have&nbsp;<a href=\"https:\/\/blog.jetbrains.com\/zh-hans\/webstorm\/2018\/02\/webstorm-2018-1-eap-181-3263\">reworked the React Native configuration<\/a>. It now gives you much more flexibility in running the bundler and building the app itself.&nbsp;In the steps below&nbsp;we use WebStorm version 2018.2.<\/p>\n<p>We\u2019ll need to create a new <em>React Native run\/debug configuration<\/em>. In the Run menu, select <em>Edit configurations\u2026<\/em>, click the <em>+<\/em> button and select <em>React Native<\/em> from the list.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-9841\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2016\/12\/webstorm-add-a-configuration.png\" alt=\"add-a-configuration\" width=\"700\" height=\"450\"><\/p>\n<p>To start your React Native app for the first time, you need to do two things: run React Native bundler\/packager and then build and launch an app on a simulator or a device using the <code>react-native run-ios<\/code> or <code>run-android command<\/code>. Only after that, you can start debugging it.<\/p>\n<p>With the&nbsp;<em>Build and Launch Application <\/em>option checked, WebStorm will do all that for you \u2013 you just need to select the target platform, iOS or Android, make sure the path to the React Native CLI package is correct and click Ok.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2016\/12\/webstorm-react-run-config.png\" alt=\"react-run-config\" width=\"700\" class=\"alignnone size-full wp-image-9849\"><\/p>\n<p>If you&#8217;re going to run your app on Android, don&#8217;t forget to start the <a href=\"https:\/\/facebook.github.io\/react-native\/docs\/getting-started.html#starting-the-android-virtual-device\" target=\"_blank\" rel=\"noopener\">Android Virtual Device<\/a>&nbsp;first. You can also run your app on a real <a href=\"https:\/\/facebook.github.io\/react-native\/docs\/running-on-device.html#running-your-app-on-android-devices\" target=\"_blank\" rel=\"noopener\">Android device connected via USB<\/a>&nbsp;(for that don&#8217;t forget to&nbsp;<a href=\"https:\/\/developer.android.com\/training\/basics\/firstapp\/running-app.html\" target=\"_blank\" rel=\"noopener\">enable USB debugging)<\/a>.<\/p>\n<p>Now let&#8217;s run the created configuration \u2013 click the green&nbsp;debug icon next to the configuration name in the IDE toolbar. WebStorm will start the <a href=\"https:\/\/github.com\/facebook\/react-native\/tree\/master\/packager\" target=\"_blank\" rel=\"noopener\">React Native packager<\/a> first in a new React Native tool window and then will run the <code>react-native run-ios<\/code> or <code>react-native run-android<\/code> command, depending on the selected target platform.<\/p>\n<p>If the build is successful, you\u2019ll see your app in an emulator.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-8689\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2016\/12\/webstorm-run-packager.png\" alt=\"run-packager\" width=\"600\"><\/p>\n<p>Once the emulator opens for the first time, go to the <a href=\"https:\/\/facebook.github.io\/react-native\/docs\/debugging.html#accessing-the-in-app-developer-menu\" target=\"_blank\" rel=\"noopener\">in-app developer menu<\/a> and select <em>Remote JS Debugging<\/em>.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-8690\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2016\/12\/webstorm-emulator.png\" alt=\"emulator\" width=\"280\"><\/p>\n<p>WebStorm\u2019s built-in debugger will then connect to the emulator, and you\u2019ll be able to hit the breakpoints you put in your source code.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-8691\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2016\/12\/webstorm-debugging-the-app.png\" alt=\"debugging-the-app\" width=\"700\"><\/p>\n<p>In the debugger you can step through your code, set watches, and explore the call stack and variable values.<\/p>\n<p>If it&#8217;s not the first time you run your app and you haven\u2019t made any changes to the native code since the last build, you don\u2019t really need to rebuild&nbsp;the app \u2013 you can just run bundler, open the app in the simulator and then debug it.<\/p>\n<p>In this case, create a new configuration (or modify the one you&#8217;ve already created) and uncheck the&nbsp;<em>Build and Launch Application<\/em> checkbox in it. When you debug this configuration, WebStorm will run React Native bundler and will wait for you to open an app in the simulator with the <em>Remote debug<\/em> enabled in the app.<\/p>\n<p><img decoding=\"async\" class=\"alignnone\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2018\/02\/webstorm-debug-react-native-app.png\" alt=\"\" width=\"700\"><\/p>\n<p>There\u2019s also now more flexibility with running React Native bundler. It\u2019s now listed as a Before Launch task in the React Native configuration. By default, it will run&nbsp;<code>react-native start<\/code>. You can also select an npm task from your project\u2019s package.json that will start the bundler.<\/p>\n<p><img decoding=\"async\" class=\"alignnone\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2018\/02\/webstorm-configure-react-native-bundler.png\" alt=\"\" width=\"700\"><\/p>\n<p>Or if you have bundler already running, you can simply remove this step from the configuration (because you don\u2019t need to run more than one bundler per app).<\/p>\n<p>If you are using Expo in your app,&nbsp;please see this <a href=\"https:\/\/blog.jetbrains.com\/zh-hans\/webstorm\/2018\/02\/webstorm-2018-1-eap-181-3263#debugging-expo\">blog post<\/a> for the steps.<\/p>\n<h2 id=\"react-native-coding-assistance\">Coding assistance<\/h2>\n<p>As you may know, WebStorm has support for React and JSX that will surely be helpful when building a React Native app. This includes: code completion for attributes, events and required properties for React components; navigation to the definition and completion for custom components; refactorings for component names; and lots more. Check out our blog post, <a href=\"https:\/\/blog.jetbrains.com\/zh-hans\/webstorm\/2015\/10\/working-with-reactjs-in-webstorm-coding-assistance\">Working with React in WebStorm: coding assistance<\/a>.<\/p>\n<p>WebStorm 2016.3 also adds code completion for React Native StyleSheet properties:<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-8692\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2016\/12\/webstorm-stylesheet-properties.png\" alt=\"stylesheet-properties\" width=\"700\"><\/p>\n<p>If you\u2019re using Flow in your project, WebStorm can highlight the errors from Flow in the editor. Enable Flow as a JavaScript version in <em>Preferences | Languages &amp; Frameworks | JavaScript<\/em> and specify a path to the Flow executable. Find out more about using Flow in this <a href=\"https:\/\/blog.jetbrains.com\/zh-hans\/webstorm\/2016\/11\/using-flow-in-webstorm\">blog post<\/a>.<\/p>\n<p><em>Your WebStorm Team<\/em><\/p>\n","protected":false},"author":221,"featured_media":0,"comment_status":"open","ping_status":"open","template":"","categories":[601],"tags":[1290,2828,2829],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/webstorm\/22589"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/webstorm"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/types\/webstorm"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/users\/221"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/comments?post=22589"}],"version-history":[{"count":2,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/webstorm\/22589\/revisions"}],"predecessor-version":[{"id":153679,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/webstorm\/22589\/revisions\/153679"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/media?parent=22589"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/categories?post=22589"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/tags?post=22589"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/cross-post-tag?post=22589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}