Categories
React Answers

How to Add Polyfills for ES6 Features in React App that is Created with create-react-app?

Spread the love

Sometimes, we need to add polyfills for ES6 features in React app that is created with create-react-app.

In this article, we’ll look at how to add polyfills for ES6 features in React app that is created with create-react-app.

Add Polyfills for ES6 Features in React App that is Created with create-react-app

To add polyfills for ES6 features in React app that is created with create-react-app, we can install the react-app-polyfill-core-js package.

To install it, we run:

npm install react-app-polyfill core-js

or

yarn add react-app-polyfill core-js

Then we create a polyfills.js file and add:

import 'react-app-polyfill/ie11';
import 'core-js/features/array/find';
import 'core-js/features/array/includes';
import 'core-js/features/number/is-nan';

at the top of the file.

This will import and run all the polyfill code to add all the required ES6 features.

And then we import the polyfills with:

import './polyfills'

in index.js.

Conclusion

To add polyfills for ES6 features in React app that is created with create-react-app, we can install the react-app-polyfill-core-js package.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *