Clue Mediator

Object doesn’t support property or method ‘remove’ in IE

📅March 28, 2020

Today we’ll show you how to fix an error Object doesn’t support property or method ‘remove’ in IE during the react application development.

Object doesn't support property or method ‘remove’ in IE react, remove() not working in all browsers, IE 11 issue - .remove() is not supported, IE11: Object doesn't support property or method 'remove', Alternative of .remove() in Internet Explorer, Crash in IE11 for javascript usage of "remove".

Checkout more articles on JavaScript

If you look at the documentation of the remove(), most of the browsers don’t support this property the same as includes() & find().

Way to Fix an error: Object doesn’t support property or method ‘remove’ in IE for ReactJS

You can use the polyfill from MDN. Add the following script at the top of all imports and it will work like charm.

We will add the code in `polyfill.js` file and import it in React project.

polyfill.js

if (global.window && global.window.Element) {
  global.window.Element.prototype.remove = function () {
    this.parentElement.removeChild(this);
  }
}
if (global.window && global.window.NodeList) {
  global.window.NodeList.prototype.remove = global.window.HTMLCollection.prototype.remove = function () {
    for (var i = this.length - 1; i >= 0; i--) {
      if (this[i] && this[i].parentElement) {
        this[i].parentElement.removeChild(this[i]);
      }
    }
  }
}

You have to import the above created `polyfill.js` file at the top of `index.js`/`App.js` file. Check the following code for your reference.

index.js

// add all script inside the polyfill file
import './polyfill.js';

import React, { Component } from "react";
import { render } from "react-dom";
import "./style.css";

class App extends Component {
  constructor() {
    super();
    this.state = {
      name: "React"
    };
  }

  componentDidMount() {
    const user = "This is test react application.";
    this.setState({
      name: user.includes("react") ? "React Application" : " Application"
    });
  }

  render() {
    return (
      <div>
        <b>Hello {this.state.name}</b>
        <p>Start editing to see some magic happen :)</p>
      </div>
    );
  }
}

render(<App />, document.getElementById("root"));

This is exactly what is needed to fix an error.

Thank you for reading. Like & Share with your friends. Happy Coding..!!