Create ReactApp
What is CRA and its benefits?
The create-react-app CLI tool allows you to quickly create & run React applications with no configuration step.
Let's create Todo App using CRA:
It includes everything we need to build a React app:
- React, JSX, ES6, and Flow syntax support.
- Language extras beyond ES6 like the object spread operator.
- Autoprefixed CSS, so you donβt need -webkit- or other prefixes.
- A fast interactive unit test runner with built-in support for coverage reporting.
- A live development server that warns about common mistakes.
- A build script to bundle JS, CSS, and images for production, with hashes and sourcemaps.
What are the approaches to include polyfills in your create-react-app?
There are approaches to include polyfills in create-react-app,
Manual import from
core-js:Create a file called (something like)
polyfills.jsand import it into rootindex.jsfile. Runnpm install core-jsoryarn add core-jsand import your specific required features.import 'core-js/fn/array/find';import 'core-js/fn/array/includes';import 'core-js/fn/number/is-nan';Using Polyfill service:
Use the polyfill.io CDN to retrieve custom, browser-specific polyfills by adding this line to
index.html:<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=default,Array.prototype.includes"></script>In the above script we had to explicitly request the
Array.prototype.includesfeature as it is not included in the default feature set.
How to use https instead of http in create-react-app?
You just need to use HTTPS=true configuration. You can edit your package.json scripts section:
or just run set HTTPS=true && npm start
How to avoid using relative path imports in create-react-app?
Create a file called .env in the project root and write the import path:
After that restart the development server. Now you should be able to import anything inside src/app without relative paths.