L
L
LB7772015-03-26 12:14:29
React
LB777, 2015-03-26 12:14:29

How to properly unmount an element (block) in react.js?

The documentation says that componentWillUnmount is fired before the element is unmounted from the DOM. But how do you unmount the element?
Simply put, I need to replace one element with another, which are tied to the same id, or simply delete the element.
How to do it competently so that componentWillUnmount would be triggered on an element that is removed from the DOM?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
1
123 123, 2015-03-26
@LB777

Don't render the component, then React will remove it from the DOM.

render() {
  let component = true ? <Comp1 /> : <Comp2 />;
  return (
    <div>{component}</div>
  );
}

P
PQR, 2015-04-03
@PQR

Let's say you first created a component and rendered it on the page like this:
Later, you can remove it from the DOM tree, plus React will clean up all event handlers on its own like this:
facebook.github.io/react/docs/top-level-api.html#r...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question