Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
157
rated 0 times [  161] [ 4]  / answers: 1 / hits: 107716  / 7 Years ago, wed, may 3, 2017, 12:00:00

I've been testing my React.js application on internet explorer, and finding that some ES6/7 code like Array.prototype.includes() breaks it.



I'm using create-react-app, and apparently they've chosen not to include a lot of polyfills since not everyone needs them, and they slow down build times (see for example here and here). The documentation (at time of writing) suggests:




If you use any other ES6+ features that need runtime support (such as
Array.from() or Symbol), make sure you are including the appropriate
polyfills manually, or that the browsers you are targeting already
support them.




So... what is the best way to 'manually' include them?


More From » reactjs

 Answers
26

Update: The create-react-app polyfill approach and docs have changed since this question/answer. You should now include react-app-polyfill (here) if you want to support older browsers like ie11. However, this only includes the ...minimum requirements and commonly used language features, so you'll still want to use one of the approaches below for less common ES6/7 features (like Array.includes)






These two approaches both work:






1. Manual imports from react-app-polyfill and core-js



Install react-app-polyfill and core-js (3.0+):



npm install react-app-polyfill core-js or yarn add react-app-polyfill core-js



Create a file called (something like) polyfills.js and import it into your root index.js file. Then import the basic react-app polyfills, plus any specific required features, like so:



/* polyfills.js */

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

/* index.js */

import './polyfills'
...





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


note, I had to explicity request the Array.prototype.includes feature as it is not included in the default feature set.


[#57915] Monday, May 1, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
beatrices

Total Points: 745
Total Questions: 103
Total Answers: 105

Location: Guam
Member since Tue, Nov 29, 2022
2 Years ago
beatrices questions
Fri, Jan 14, 22, 00:00, 2 Years ago
Sun, Feb 14, 21, 00:00, 3 Years ago
Fri, Nov 20, 20, 00:00, 4 Years ago
Mon, Aug 12, 19, 00:00, 5 Years ago
;