Unmounting React.js node

前端 未结 6 471
北海茫月
北海茫月 2020-12-07 18:36

I\'m trying to unmount a React.js node with this._rootNodeID

 handleClick: function() {

        React.unmountComponentAtNode(this.         


        
相关标签:
6条回答
  • 2020-12-07 19:06

    As mentioned in the GitHub issue you filed, if you want access to a component's DOM node, you can use this.getDOMNode(). However a component can not unmount itself. See Michael's answer for the correct way to do it.

    0 讨论(0)
  • 2020-12-07 19:09

    This worked for me. You may want to take extra precautions if findDOMNode returns null.

    ReactDOM.unmountComponentAtNode(ReactDOM.findDOMNode(this).parentNode);
    
    0 讨论(0)
  • 2020-12-07 19:10

    The example I use:

    unmount: function() {
      var node = this.getDOMNode();
      React.unmountComponentAtNode(node);
      $(node).remove();
    },
    
    handleClick: function() {
      this.unmount();
    }
    
    0 讨论(0)
  • 2020-12-07 19:30

    Unmount components from the same DOM element that you mount them in. So if you did something like:

    ReactDOM.render(<SampleComponent />, document.getElementById('container'));
    

    Then you would unmount it with:

    ReactDOM.unmountComponentAtNode(document.getElementById('container'));
    

    Here is a simple JSFiddle where we mount the component and then unmount it after 3 seconds.

    0 讨论(0)
  • 2020-12-07 19:30

    First , i am new to reactjs ,too . Of course we can control the Component all by switch the state , but as I try and test , i get that , the React.unmountComponentAtNode(parentNode) can only unmount the component which is rendered by React.render(<SubComponent>,parentNode). So <SubComponent> to be removed must be appened by React.render() method , so I write the code

    <script type="text/jsx">
    
        var SubComponent = React.createClass({
            render:function(){
                return (
                        <div><h1>SubComponent to be unmouned</h1></div>
                );
            },
            componentWillMount:function(){
                console.log("componentWillMount");
            },
            componentDidMount:function(){
                console.log("componentDidMount");
            },
            componentWillUnmount:function(){
                console.log("componentWillUnmount");
            }
    
        });
    
        var App = React.createClass({
    
            unmountSubComponent:function(){
                var node = React.findDOMNode(this.subCom);
                var container = node.parentNode;
                React.unmountComponentAtNode(container);
                container.parentNode.removeChild(container)
            },
    
            componentDidMount:function(){
                var el = React.findDOMNode(this)
                var container = el.querySelector('.container');
                this.subCom = React.render(<SubComponent/> ,  container);
            },
    
            render:function(){
    
                return (
                    <div className="app">
                        <div className="container"></div>
                        <button onClick={this.unmountSubComponent}>Unmount</button>
                    </div>
                )
            }
        });
    
        React.render(<App/> , document.body);
    </script>
    

    Run the sample code in jsFiddle , and have a try .

    Note: in the sample code React.findDOMNode is replaced by getDOMNode as the reactjs version problem .

    0 讨论(0)
  • 2020-12-07 19:32

    You don't need to unmount the component the simple solution it's change the state and render a empty div

    const AlertMessages = React.createClass({
      getInitialState() {
        return {
          alertVisible: true
        };
      },
      handleAlertDismiss() {
        this.setState({alertVisible: false});
      },
      render() {
        if (this.state.alertVisible) {
          return (
            <Alert bsStyle="danger" onDismiss={this.handleAlertDismiss}>
              <h4>Oh snap! You got an error!</h4>
            </Alert>
          );
        }
        return <div></div>
      }
    });
    
    0 讨论(0)
提交回复
热议问题