React: Why does Babel not polyfill String.prototype.replaceAll?

Viewed 3417

Having read that React takes care of setting up Babel and webpack for me, I used the new (ES2020 or ES2021?) String.prototype.replaceAll() function in my code, assuming it would be polyfilled by Babel for unsupported browsers, but it isn't.

Is there extra config I can add to make Babel do this polyfill? Adding extra config seems to be against the philosophy of create-react-app.

1 Answers

Thanks to @jonrsharpe, following a link in the documentation he pointed to to react-app-polyfill.

I added the react-app-polyfill node module to my app, and added:

import 'react-app-polyfill/ie9';
import 'react-app-polyfill/stable';

to my index.js file. This will, hopefully, do some polyflling for older browsers, but there's no polyfill for String.prototype.replaceAll(), and I haven't been able to test if it does actually help older browsers use my site that wouldn't otherwise be able to.

Related