WebStorm 2019.1 Help

React

React is a JavaScript library for building complex interactive User Interfaces from encapsulated components. Learn more about the library from the React official website.

WebStorm integrates with React providing assistance in configuring, editing, linting, running, debugging, and maintaining your applications.

Creating a new React application

You can use the create-react-app package or create an empty WebStorm project and install React in it.

Generating a React application with create-react-app

Create React App is the recommended way to start building a new React single page application. As a result, your development environment is preconfigured to use webpack, Babel, ESLint, and other tools. Learn more about installing React and creating React applications from React official website.

To install create-react-app globally

  • Open the built-in Terminal (View | Tool Windows | Terminal) and type npm install -g create-react-app at the command prompt.

To create an application

  1. Select File | New | Project from the main menu or click Create New Project on the Welcome screen. The Create New Project Dialog opens.

  2. In the left-hand pane, choose React App.

  3. In the right-hand pane, specify the project folder, the Node.js interpreter to use, and the path to the create-react-app package.

  4. Optionally:
    Specify a custom package to use instead of react-scripts during the project generation. This can be one of the packages forked from react-scripts, for example, react-awesome-scripts, custom-react-scripts, react-scripts-ts, and so on.

  5. When you click Create, WebStorm generates a React-specific project with all the required configuration files and downloads the required dependencies. WebStorm also creates an npm start and JavaScript Debug configurations with default settings for running or debugging your application.

To download the project dependencies, do one of the following:

  • In the embedded Terminal (Alt+F12), type:

    npm install

  • Select Run 'npm install' from the context menu of the package.json file in your project root.

Installing React in an empty WebStorm project

In this case, you will have to configure the build pipeline yourself as described in Building a React application below. Learn more about adding React to a project from the React official website.

To create an empty WebStorm project

  1. Select File | New | Project from the main menu or click Create New Project on the Welcome screen. The Create New Project Dialog opens.

  2. In the left-hand pane, choose Empty Project. In the right-hand pane, specify the application folder and click Create.

To install React in an empty project

  1. Open the empty project where you will use React.

  2. In the embedded Terminal (Alt+F12), type:

    npm install --save react react-dom

Starting with an existing React application

To continue developing an existing React application, open it in WebStorm and download the required dependencies.

To open the application sources that are already on your machine

  • Click Open on the Welcome screen or select File | Open from the main menu. In the dialog that opens, select the folder where your sources are stored.

To check out the application sources from your version control

  1. Click Check out from Version Control on the Welcome screen or select VCS | Check out from Version Control from the main menu.

  2. Select your version control system from the list.

  3. In the VCS-specific dialog that opens, type your credentials and the repository to check out the application sources from.

To download the dependencies

  • Click Run 'npm install' in the popup:

    Opening an Angular application and downloading the dependencies from package.json

Completing code

WebStorm provides code completion for React APIs and JSX in JavaScript code. Code completion works for React methods, React-specific attributes, HTML tags and component names, React events, component properties, and so on. Learn more from the React official website.

To get code completion for React methods and React-specific attributes, you need to have the react.js library file somewhere in your project. Usually the library is already in your node_modules folder.

Completing React methods, attributes, and events

By default, the code completion popup is displayed automatically as you type. For example:

Completion popup
In JSX tags, WebStorm provides coding assistance for React-specific attributes, such as className or classID, and non-DOM attributes, such as key or ref. Moreover, auto-completion also works for names of classes defined in the project’s CSS files:
All React events, such as onClick or onChange, can also be completed automatically together with curly braces (={}):
ws_react_events.png
Completion also works for JavaScript expressions inside curly braces. This applies to all the methods and functions that you have defined:
ws_react_javascript_expression.png

Completing HTML tags and component names

WebStorm provides code completion for HTML tags and component names that you have defined inside methods in JavaScript or inside other components:

ws_react_component_completion.png
Completion also works for imported components with ES6 style syntax:
ws_react_imported_component_completion.png

Completing component properties

WebStorm provides code completion for component properties defined using propTypes and resolves them so you can quickly jump or preview their definitions:

ws_react_component_properties.png
When you autocomplete the name of a component, WebStorm adds all its required properties automatically. If some of the required properties are missing in the usage of a component, WebStorm warns you about that.

Transferring HTML attributes to JSX

When you copy a piece of HTML code with class attributes or on-event handlers and paste it into JSX, WebStorm automatically replaces these attributes with React-specific ones (className, onClick, onChange, and so on.)

To copy HTML code to JSX "as is", use Paste Simple (Ctrl+Shift+Alt+V).

Using React code snippets

WebStorm comes with a collection of more than 50 code snippets that expand into different statements and blocks of code often used in React apps. The example below shows how you can use the rcjc abbreviation to create a class that defines a new React component:

To create a React code construct from a snippet

  • Type the required abbreviation in the editor and press Tab.

  • Press Ctrl+J and choose the relevant snippet. To narrow down the search, start typing the abbreviation and then select it from the completion list.

To view the list of all available React snippets

  • In the Settings/Preferences dialog (Ctrl+Alt+S), click Live Templates under Editor, and then expand the React node.

Using Emmet in JSX

With WebStorm, you can use Emmet not only in HTML but also in your JSX code taking advantage of some special React twists. For example, the abbreviation div.my-class expands in JSX to <div className=”my-class"></div> but not to <div class=”my-class"></div> as it would in HTML:

Navigating through a React application

Besides the basic navigation, WebStorm helps you jump between React-specific code elements.

  • To jump to the definition of a method or a JavaScript expression inside curly braces {}, select the method or expression and press Ctrl+B.

  • To jump to the definition of a component, select the component name and press Ctrl+B.

  • To view documentation for a component, press Ctrl+Shift+I.

ws_react_quick_definition.png

WebStorm lets you easily navigate through JSX tags using breadcrumbs and colorful highlighting for the tag tree in the editor gutter:

Highlighting and breadcrumbs for JSX tags in a React application

Linting a React application

All the WebStorm built-in code inspections for JavaScript and HTML also work in JSX code. WebStorm alerts you in case of unused variables and functions, missing closing tags, missing statements, and much more:

ws_react_inspection.png
For some inspections WebStorm provides quick-fixes, for example, suggests adding a missing method. To view the quick-fix popup, press Alt+Enter.

Using ESLint

Besides providing built-in code inspections, WebStorm also integrates with linters, such as ESLint, for JSX code. ESLint brings a wide range of linting rules that can also be extended with plugins. WebStorm shows warnings and errors reported by ESLint right in the editor, as you type. With ESLint, you can also use JavaScript Standard Style.

To have ESLint properly understand React JSX syntax, you need eslint-plugin-react. With this plugin, you are warned, for example, when the display name is not set for a React component, or when some dangerous JSX properties are used:

ws_eslint_react.png

To get started with ESLint in WebStorm

  1. In the built-in Terminal (View | Tool Windows | Terminal), type npm install --save-dev eslint and npm install --save-dev eslint-plugin-react.

  2. Add a ESLint configuration file .eslintrc to your project.

  3. In the Settings/Preferences dialog (Ctrl+Alt+S), go to Languages and Frameworks | JavaScript | Code Quality Tools | ESLint. On the ESLint page that opens, select Automatic ESLint configuration. WebStorm will automatically locate ESLint in your project’s node_modules folder and then use the .eslintrc configuration by default.

    You can also configure ESLint manually to use a custom ESLint package and configuration.

ws_eslint_enable.png

Example of .eslintrc structure (ESLint 1.x with react plugin)

  • In the ecmaFeatures object, add "jsx" = true. Here you can also specify additional language features you’d like to use, for example ES6 classes, modules, and so on.

  • In the plugins object, add react.

  • In the rules object, you can list ESLint built-in rules that you would like to enable, as well as rules available via the react plugin.

{ "parser": "babel-eslint", "env": { "browser": true }, "ecmaFeatures": { "jsx": true }, "plugins": [ "react" ], "rules": { "semi": 2 } }
Learn more about ESLint and react plugin configuration from ESLint official website.

Refactoring a React application

Besides the common WebStorm refactorings, in a React application you can also run Rename for React components and use Extract Component to create new components.

Rename a component

  • Place the cursor within the component name and press Shift+F6

Below is an example of renaming a component that is defined and used in only one file:

In the same way, you can rename components defined in one file and then imported to another file using a named export:

Extract a component

You can create a new React component by extracting the JSX code from the render method of an existing component.

  1. Select the code you want to extract and choose Refactor | Extract |Component from the main menu or from the context menu.

  2. Name the component.

  3. Select Class if you want to define the component as a class, or Function if you want to create a functional component.

  4. Click OK. The new component will be defined next to the existing one and used in it.

Destructuring in a React application

Destructuring lets you easily unpack values from arrays and objects into variables. This functionality has a very concise syntax that is often used when you need to pass data in your application.

In WebStorm, you can simplify fragments of code in which you are getting multiple values out of an array or an objects using a dedicated refactoring, intention action, or quick-fix. When working with React class components, consider using the Introduce object or array destructuring intention action.

Running and debugging a React application

The recommended way to start building a new React single page application is Create React App. Only in this case your development environment is preconfigured to use webpack and Babel. Otherwise, you need to configure a build pipeline first.

To run a React application, do one of the following:

  • In the npm tool window (View | Tool Windows | npm), double-click the start task.

    Thanks to the Webpack Hot Module Replacement, when the development server is running, your application is automatically reloaded as soon as you change any of the source files and save the updates.

To debug a React application

  1. Start the application in the development mode by double-clicking the start task in the npm tool window.

  2. Wait till the application is compiled and the Webpack development server is ready. Open your browser at http://localhost:3000/ to view the application.

  3. Copy the URL address at which the application is running (http://localhost:3000/ by default), you will later need this URL when creating a debug configuration.

    ws_react_debug_cra_app_is_running.png

  4. Create a new JavaScript debug configuration: choose Run | Edit Configurations, click Add icon, and choose JavaScript Debug from the list. In the Run/Debug Configuration: JavaScript Debug dialog, paste the saved URL (http://localhost:3000/) in the URL field. Save the configuration.

  5. Set the breakpoints in your code and start a debugging session by clicking the Debug button next to the list of configurations.

  6. When the first breakpoint is hit, switch to the Debug Tool Window and proceed as usual: step through the program, stop and resume program execution, examine it when suspended, explore the call stack and variables, set watches, evaluate variables, view actual HTML DOM, and so on.

    ws_react_debug_cra_debug_session.png

Building a React application

You need to set up the build process if you installed React in an existing WebStorm project. Learn about various ways to configure a build pipeline for your React application from React official website.

Testing a React application

You can run and debug Jest tests in React applications created with create-react-app. Before you start, make sure the react-scripts package is added to the dependencies object of your package.json.

You can run and debug Jest tests via a run/debug configuration, or right from the editor, or from the Project tool window, see Jest for details.

To create a Jest run/debug configuration

  1. Open the Run/Debug Configuration dialog (Run | Edit Configurations on the main menu), click the Add button in the left-hand pane, and select Jest from the list. The Run/Debug Configuration: Jest dialog opens.

  2. Specify the Node interpreter to use and the working directory of the application. By default, the Working directory field shows the project root folder. To change this predefined setting, specify the path to the desired folder or choose a previously used folder from the list.

  3. In the Jest package field, specify the path to the react-scripts package.

  4. In the Jest options field, type --env=jsdom.

    ws_react_run_config_jest.png

To run tests

  1. Select the Jest run/debug configuration from the list on the main toolbar and click Run with Coverage to the right of the list.

    ws_select_run_configuration_jest.png

  2. The test server starts automatically without any steps from your side. View and analyze messages from the test server in the Run tool window.

  3. Monitor test execution in the Test Runner tab of the Run tool window.

To debug tests

  1. Select the Jest run/debug configuration from the list on the main toolbar and click the Debug button to the right of the list.

  2. In the Debug Tool Window that opens, proceed as usual: step through the tests, stop and resume test execution, examine the test when suspended, and so on.

Some known limitations

  • When you open an application during a debugging session for the first time, it may happen that some of the breakpoints in the code executed on page load are not hit. The reason is that to stop on a breakpoint in the original source code, WebStorm needs to get the source maps from the browser. However the browser can pass these source maps only after the page has been fully loaded at least once. As a workaround, reload the page in the browser yourself.

  • If you are using webpack-dev-server from Webpack version earlier than 2, it is recommended that you disable the Safe write feature in WebStorm. Otherwise the application won’t be updated on-time when changed. This issue is fixed in Webpack 2.

Last modified: 16 May 2019

See Also