Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How component can remove/delete itself from DOM?

Let's say I have a following JSX code:

var List = React.createClass({
  render: function() {
    var Items = this.props.data.map(function(item) {
      return (
        <Item key={item.id}>
          {item.text}
        </Item>
      );
    });
    return (
      <div className="items">
        {Items}
      </div>
    );
  }
});

var Item = React.createClass({
  handleDelete: function(e) {
    e.preventDefault();

    // Delete this (only this) component completely (remove it from DOM).
  },
  render: function() {
    return (
      <div className="item">
          {this.props.children}
          <button className="delete" onClick={this.handleDelete}>Delete</button>
      </div>
    );
  }
});

ReactDOM.render(
  <List data={Array of items} />,
  document.getElementById('content')
);

My question is: how items can completely remove/delete themselves in their own handleDelete() function?

like image 536
balazswmann Avatar asked Mar 14 '16 17:03

balazswmann


People also ask

When a component is being removed from the DOM?

Unmounting. The next phase in the lifecycle is when a component is removed from the DOM, or unmounting as React likes to call it. React has only one built-in method that gets called when a component is unmounted: componentWillUnmount()

Can a react component unmount itself?

React is intended to have an unmount happen from a parent to child relationship. Now if you want a child to unmount itself, you can simulate this with a state change in the parent that is triggered by the child.

How remove element from DOM react?

Remove stand-alone element onclick in ReactAttach an event handler to the onClick event of the element. In the event handler, negate the value of the visibility state to remove the element from the DOM.

What is used to delete an HTML element from DOM?

HTML DOM Element remove() The remove() method removes an element (or node) from the document.


2 Answers

One solution would be to have the container component (ie List) handle the onDelete functionality, and then update its state (keep track of deleted items). Then in your List's render() function, you can filter out any deleted items:

var List = React.createClass({
  getInitialState: function () {
    return { deleted: [] };
  },

  onDelete: function (id) {
    this.setState({ deleted: this.state.deleted.concat([id]) });
  },

  render: function() {
    var Items = this.props.data
      .filter(item => this.state.deleted.indexOf(item.id) === -1)
      .map(item => {
        return (
          <Item key={item.id} id={item.id} onDelete={id => this.onDelete(id)}>
            {item.text}
          </Item>
        );
      });

    return (
      <div className="items">
        {Items}
      </div>
    );
  }
});

var Item = React.createClass({
  render: function() {
    return (
      <div className="item">
          {this.props.children}
          <button className="delete" onClick={() => this.props.onDelete(this.props.id)}>Delete</button>
      </div>
    );
  }
});

ReactDOM.render(
  <List data={Array of items} />,
  document.getElementById('content')
);
like image 92
Calvin Belden Avatar answered Oct 03 '22 03:10

Calvin Belden


if you want to delete it from the child component itself then you may try this:

ReactDOM.unmountComponentAtNode(ReactDOM.findDOMNode(this).parentNode);

but its always better for parent to manipulate the child components rather than child components modifying itself. So, you should put the logic inside your parent component to mount the required number of child components, and child should only communicate to parent.

EDIT

i do not recommend to use the above method for the asked question, i wrote this answer because this is one of the way to unmount your component. Here is the official doc. Its completely fine to use this method, when you want to remove some child components from parent component that's why ReactDOM allows us to use this function.

like image 20
Vikramaditya Avatar answered Oct 03 '22 02:10

Vikramaditya