Create React App 2.1, released at the end of October 2018, added official support for TypeScript. This, along with all of the improvements and features brought by the second version of Create React App, removes much of the complexity of creating and configuring a React app.
In this article, I’m going to cover the following features of Create React App 2:
For a complete list of all of the features (and some breaking changes), you can check out the official post about this version and the changelog of the project.
For new applications, you only have to execute create-react-app with --typescript
option:
npx create-react-app sample-typescript-app --typescript
If npx is not executing the latest version (2.1.1 at the time of this writing), specify the version of create-react-app:
npx [email protected] sample-typescript-app --typescript
This will install the packages related to TypeScript and create a default tsconfig.json
file. Here’s an extract of the command’s output:
Installing react, react-dom, and react-scripts... + [email protected] + [email protected] + [email protected] + @types/[email protected] + @types/[email protected] + @types/[email protected] + @types/[email protected] + [email protected] ... We detected TypeScript in your project (srcApp.test.tsx) and created a tsconfig.json file for you. Your tsconfig.json has been populated with default values.
The file src/App.tsx
will be identical to its JavaScript counterpart, however, if, for example, you add a functional component to the file like this:
An error concerning the props
parameter type will be thrown:
Because now, you have to declare a type definition using React.SFC:
interface ContentProps {
title: string,
text: string,
}
const Content: React.SFC<ContentProps> = (props) => {
return <div><h1>{props.title}</h1>{props.text}</div>;
}
And if your IDE supports it (Visual Studio Code in the image below), it will show you the type of the component’s attribute and it will also catch type errors:
On the other hand, to add TypeScript to an existing Create React App app, first, manually install the TypeScript-related packages:
npm install --save typescript @types/node @types/react @types/react-dom @types/jest
Next, change the extension of the files from *.js
to *.tsx
and restart the application (if it’s running).
The structure of the project created by Create React App remains the same.
The only changes are related to service workers.
First, the file src/registerServiceWorker.js
was renamed to src/serviceWorker.js
and now, inside of this file, a config
object is passed to the function registerValidSW
to enable the callbacks onUpdate
and onSuccess
:
The second change is in src/index.js
, the service worker is unregistered by default:
You can learn more about service workers and progressive web apps here.
Create React App 2 gives you more options to style your application without the need for complex configurations, ejecting the application or using react-app-rewired.
The first option is Sass. To use it, first install node-sass:
npm install --save node-sass
Now you can create a Sass file (src/Message.scss
). For example:
And use it in a component (src/Message.js
) this way:
On the other hand, with CSS Modules, you don’t have to install more dependencies, you just have to follow the [name].module.css
file naming convention.
For example, if you have a Message.js
component (shown in the previous example), you can define its styles in the file Message.module.css
(remember that with CSS Modules you can compose classes):
And in the component, use the styles like this:
This way, at runtime, a CSS class with a semi-random name will be generated to locally scope those styles to the component:
<li class="Message_my-message_tp3lv"></li>
Also, postcss-flexbugs-fixes and postcss-preset-env (supporting only stage3+ features) are included in Create React App 2, which means vendor prefixes are added automatically and new CSS features for older browsers are polyfilled. You can learn more about it here.
It’s been a while since React 16.2 added support for fragments to return multiple children from a component’s render method:
However, as the JSX Fragment Syntax (or short syntax):
It’s only supported by Babel 7, you can use it now that Create React App 2 uses this version of Babel.
However, remember that <></>
is just syntax sugar for <React.Fragment>
but without the option to use keys or other attributes.
In the context of Create React App 2, Babel macros allow you to use a special type of Babel plugins without any configuration.
Let me explain.
Babel plugins allow you to manipulate and transform code at build time. For example, there’s a plugin that transforms arrow functions:
const a = (b) => b;
To regular JavaScript functions:
const a = function (b) { return b; };
However, one problem with Babel plugins is that you have to configure each one you want to use. Usually, this is done in .babel.rc
, but when using Create React App, you don’t have access to this file unless you eject the application.
Luckily, there is babel-plugin-macros, a plugin that defines a standard interface for…macros (i.e. plugins that perform build-time transformations).
This way, you only have to add this plugin to your project (which is what Create React App does) to use any number of macros you want.
And since macros are processed at build-time and not required at runtime, they should be specified as devDependencies
.
You can search npm for the keyword babel-plugin-macros to find macros.
Take, for example, the Babel plugin/macro tagged-translations, which translates text at build-time.
Just add this library to a Create React App project with:
npm install --save-dev tagged-translations
Add the file translations/default.json
at the root of the project with a translation like the following:
{ "Hello world": "Hola mundo" }
And use it as a macro in your components (not as a plugin, there’s a difference):
As you can see in the following image, the translation happens when the bundle is created at build-time:
In the first version of Create React App, when making an API request you either had the option of hard-coding the complete URL of the request like this:
Or add a proxy field to the package.json
file:
'proxy': 'http://localhost:3001/'
To just use the path of the resource in the fetch
call:
With Create React App 2, in addition to the methods shown above, you can configure a custom proxy by installing http-proxy-middleware:
npm install --save http-proxy-middleware
And creating the file src/setupProxy.js
to configure the proxy:
This file will be imported automatically when the application starts, and it gives you access to an Express instance to do something like this:
Yarn Plug’n’Play mode allows your application to work without a node_modules
directory.
This way, the app dependencies are loaded from Yarn’s cache, rather than requiring copying them into the node_modules
directory during the installation step. This has the added benefit of faster app creations.
To use this feature, you’ll need Yarn 1.12+, Node.js 8.9+, and be sure to create your React application with the option --use-pnp
:
npx create-react-app light-app --use-pnp
If the command executed successfully, in the file package.json
, you will find an additional configuration option:
{ ... "installConfig": { "pnp": true }, ... }
Also, you’ll get a .pnp
directory with links to directories of Yarn’s cache and the file .pnp.js
, which validates dependencies and provides the ability to search for them from the global cache instead of the node_modules
directory.
This is an experimental feature(at the time of this writing) so it might not work in all situations (for example, I didn’t work on my Windows 10 machine) and with tools that work with the node_modules
directory directly.
Create React App 2 adds more options to ease the creation and configuration of React applications. Personally, the three new features I find most helpful are TypeScript, Saas and Babel macros.
However, I have to say that for an existing application, if you don’t need these new features or if you have a working configuration using react-app-rewired or craco, it’s probably not worth upgrading.
Even the Create React App team advise this:
Don’t feel pressured to upgrade anything. If you’re satisfied with the current feature set, its performance, and reliability, you can keep using the version you’re currently at! It might also be a good idea to let the 2.0 release stabilize a little bit before switching to it in production.
But for new applications, you have many new features at your disposal.
Happy hacking!
Would you be interested in joining LogRocket's developer community?
Join LogRocket’s Content Advisory Board. You’ll help inform the type of content we create and get access to exclusive meetups, social accreditation, and swag.
Sign up nowIn this guide, explore lazy loading and error loading as two techniques for fetching data in React apps.
Deno is a popular JavaScript runtime, and it recently launched version 2.0 with several new features, bug fixes, and improvements […]
Generate OpenAPI API clients in Angular to speed up frontend development, reduce errors, and ensure consistency with this hands-on guide.
Making carousels can be time-consuming, but it doesn’t have to be. Learn how to use React Snap Carousel to simplify the process.