AppCode 2020.3 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.

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

Before you start

  1. Make sure you have Node.js on your computer.

  2. Make sure the JavaScript and TypeScript bundled plugin is enabled on the Settings/Preferences | Plugins page, see Managing plugins for details.

Starting with an existing React application

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

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.

Check out the application sources from your version control

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

  2. In the invoked dialog, select your version control system from the list and specify the repository to check out the application sources from.

Download the dependencies

Completing code

AppCode 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, AppCode 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:

Completion for class names

All React events, such as onClick or onChange, can also be completed automatically together with curly braces  ={}:

Complete React events

Completion also works for JavaScript expressions inside curly braces. This applies to all the methods and functions that you have defined:

Completing JavaScript expressions inside curly braces

Completing HTML tags and component names

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

Completion for HTML tags and component names

Completion also works for imported components with ES6 style syntax:

Completion for imported components with ES6 syntax

Completing component properties

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

Completion for component properties

When you autocomplete the name of a component, AppCode adds all its required properties automatically. If some of the required properties are missing in the usage of a component, AppCode 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, AppCode automatically replaces these attributes with React-specific ones (className, onClick, onChange, and so on.)

Transfer HTML to JSX

This also works for TSX:

Transfer HTML to TSX

To copy HTML code to JSX or TSX "as is", use Paste Simple ⌃⌥⇧V or open the Preferences dialog ⌃⌥S, go to Editor | General | Smart Keys | JavaScript, and clear the Convert attributes when pasting HTML to JSX files checkbox.

Using React code snippets

AppCode 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:

Live template for a React component

Create a React code construct from a snippet

  • Type the required abbreviation in the editor and press .

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

See Live Templates for details.

View the list of all available React snippets

  • In the Preferences dialog ⌃⌥S, click Live Templates under Editor, and then expand the React node.

Using Emmet in JSX

With AppCode, 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:

Expand Emmet template in React

Navigating through a React application

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

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

  • To jump to the declaration of a component, select the component name and press ⌃B. Learn more from Go to declaration and its type.

  • To view component definition, press ⌃⇧I.

    View quick definition of a component in a popup
    View quick definition of a component in a popup
  • To view quick documentation for a component, press ⌃Q. Learn more from JavaScript documentation look-up.

    Quick documentation popup for React component
  • AppCode lets you easily navigate through JSX tags using breadcrumbs and colorful highlighting for the tag tree in the editor gutter. See Navigating with breadcrumbs for details.

    Highlighting and breadcrumbs for JSX tags in a React application

Linting a React application

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

JavaSxript inspections in React application

For some inspections AppCode provides quick-fixes, for example, suggests adding a missing method. To view the quick-fix popup, press ⌥⏎.

To customize the list of inspections, open the Preferences dialog ⌃⌥S, go to Editor | Inspections, and disable the inspections you don’t want to see or change their severity levels. Learn more from Disabling and enabling inspections and Configuring inspection severities.

Using ESLint

Besides providing built-in code inspections, AppCode 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. AppCode shows warnings and errors reported by ESLint right in the editor, as you type. With ESLint, you can also use JavaScript Standard Style.

See ESLint for details.

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:

ESLint with React: errors and warnings are highlighted, the description of a problem is shown in a tooltip.

Install and configure ESLint in a React project

  1. In the built-in Terminal (View | Tool Windows | Terminal ), type:

    npm install --save-dev eslint
    npm install --save-dev eslint-plugin-react
  2. Add a ESLint configuration file .eslintrc.* to your project. This can be a .eslintrc, .eslintrc.json, or .eslintrc.yaml file, or a file in another supported format, see the ESLint official website for details.

  3. In the Preferences dialog ⌃⌥S, go to Languages and Frameworks | JavaScript | Code Quality Tools | ESLint, and select Automatic ESLint configuration. AppCode will automatically locate ESLint in your project node_modules folder, and then use the default configuration from .eslintrc.* file or from eslintConfig property in a package.json.

    Alternatively, select Manual ESLint configuration to use a custom ESLint package and configuration.

    See Activating and configuring ESLint in AppCode for details.

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

  1. 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.

  2. In the plugins object, add react.

  3. 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 the ESLint official website.

Refactoring a React application

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

Rename a component

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

Rename component used in one file

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

Rename a component defined in another file and imported through a named import
  1. Place the caret within the component name and press ⇧F6 or select Refactor | Rename from the main menu of from the context menu.

  2. Specify the new component name in compliance with React naming conventions.

Extract a component

You can create a new React component by extracting the JSX code from the render method of an existing component. The new component can be defined as a function or as a class, see Function and Class Components on the React official website.

Extract a React component
  1. Select the code you want to extract and choose Refactor | Extract Component from the context menu.

    Alternatively, go to Refactor | Extract/Introduce | Extract Component on the main menu or press ⌃⌥⇧T and select Extract Component from the popup.

  2. In the dialog that opens, specify the name of the new component and its type. By default, a functional component is created. If you want to define the new component as a class, select Class.

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

  4. Optionally: use the Move Symbol refactoring to move the new component and all the required imports to a separate file.

  5. Optionally: modify the code templates that AppCode uses for new components. In the Preferences dialog ⌃⌥S, go to Editor | File and Code Templates, open the Code tab, and update the templates as necessary using the Apache Velocity template language.

Convert a function to a class component

With the Convert to Class Component refactoring, AppCode generates a ES6 class with the name of the function which you want to convert. This class extends React .Component and contains a render() method where the function body is moved. Learn more from the React official website.

Convert a function to a class component
  • Place the caret anywhere inside the function to convert and select Refactor | Convert to Class Component from the main menu or from the context menu.

  • Alternatively, press ⌃⌥⇧T and select Convert to Class Component from the popup.

Convert a class to a functional component

With the Convert to Functional Component refactoring, AppCode generates a function with the name of the class which you want to convert and moves the contents of the render() method to the function body.

Convert a class to a functional component
  • Place the caret anywhere inside the class to convert and select Refactor | Convert to Functional Component from the main menu or from the context menu.

  • Alternatively, press ⌃⌥⇧T and select Convert to Functional Component from the popup.

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.

When working with React class components, consider using the Introduce object/array destructuring intention action. Learn more from Destructuring in JavaScript.

Destructuring with intention action: Introduce object destructuring in a React class

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.

For applications created with Create React App, AppCode generates two run/debug configurations with default settings:

  • An npm configuration with the default name npm start. This configuration runs the npm start command that launches the development server and starts your application in the development mode.

  • A JavaScript Debug configuration with the default name Debug Application. This configuration launches a debugging session.

Run a React application

  1. Select the npm start run configuration from the list on the toolbar and click Run next to the list.

    Alternatively, run npm start in the Terminal ⌥F12 or double-click the start task in the npm tool window (View | Tool Windows | npm ).

  2. Wait till the application is compiled and the Webpack development server is ready.

    The Run tool window or the Terminal shows the URL at which your application is running, by default it is http://localhost:3000/. Click this link to view the application.

    React app is running

Debug a React application

  1. Set the breakpoints in your code.

  2. Start the application in the development mode as described above and wait till the application is compiled and the development server is ready.

  3. Select the autogenerated Debug Application configuration from the list and click the Debug button next to the list.

    Start debugging an React app with Debug Application configuration

You can start a debugging session in different ways depending on where your application is running.

Debug applications running on localhost

  1. Set the breakpoints in your code.

  2. Start the application in the development mode as described above and wait till the application is compiled and the Webpack development server is ready.

  3. The Run tool window or the Terminal shows the URL at which your application is running, by default it is http://localhost:3000/. Hold Ctrl+Shift and click this URL link. AppCode starts a debugging session with an automatically generated Debug Application configuration of the type JavaScript Debug.

    Start debugging a React app from the Run tool window

Debug applications running on custom URLs

  1. Set the breakpoints in your code.

  2. Start the application in the development mode as described above and wait till the application is compiled and the Webpack development server is ready.

  3. The Run tool window or the Terminal shows the URL at which your application is running. Copy this URL address, you will later specify it in a debug configuration. To view your application, just click the link.

  4. Create a JavaScript Debug configuration. To do that, go to Run | Edit Configurations on the main menu, click the Add icon, and select JavaScript Debug from the list. In the Run/Debug Configuration: JavaScript Debug dialog, paste the saved URL in the URL field and save the configuration.

  5. To launch your newly created configuration, select it from the list of configurations and click the Debug button next to the list.

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.

Building a React application

You need to set up the build process if you installed React in an existing AppCode 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.

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.

    Testing React: Jest run configuration

Run tests

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

  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.

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.

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, AppCode 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.

Last modified: 08 March 2021