javascript - Reactjs 0.13 未调用 componentWillUnMount

标签 javascript reactjs

代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name=description content="">
    <meta name=viewport content="width=device-width, initial-scale=1">
    <title>Component Lifecycle: Mounting</title>
    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.0-beta.1/react.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.0-beta.1/JSXTransformer.js"></script>
    <style type="text/css">
        body{margin: 25px;}
    </style>
</head>
<body>
    <button onClick="render()">Render</button>
    <button onClick="unmount()">Unmount</button>
    <hr />
    <div id="panel"></div>

    <script type="text/jsx;harmony=true">
    /** @jsx React.DOM */
    var APP = React.createClass({
        update:function() {
            var newVal = this.props.val + 1;
            this.setProps({val:newVal});
        },
        componentWillMount:function() {
            this.setState({m:2});
            if (this.props.val === 0) {
                this.btnStyle = {'color' : 'red'};
            }
        },
        render: function() {
            console.log("hello world");
            return <button 
                        style={this.btnStyle}
                        onClick={this.update}>
                        {this.props.val*this.state.m}
                    </button>
        },
        componentDidMount:function(rootNode) {
            this.inc = setInterval(this.update, 500);
        },
        componentWillUnMount:function() {
            console.log("goodbye cruel world!");
            clearInterval(this.inc);
        }
    });
    window.render = function() {
        React.render(
            <APP val={0} />,
            document.getElementById('panel')
        );  
    };

    window.unmount = function() {
        React.unmountComponentAtNode(
            document.getElementById('panel')
        );  
    };

    </script>
</body>
</html>

我的结果:

enter image description here

如您所见,不知何故,为了让我清除Interval,未调用卸载。不过其他一切都正常。

我哪里出错了?

最佳答案

将 Brandon Pugh 在评论中的回答正式化:

方法是componentWillUnmount .

问题是由于大写的M:

        ...
    },
    componentWillUnMount:function() { // <- should be componentWillUnmount
        console.log("goodbye cruel world!");
        ...

关于javascript - Reactjs 0.13 未调用 componentWillUnMount,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28955140/

相关文章:

javascript - 在 react 中渲染输入数组

javascript - ReactJS router v4 Mobx - 路由器不会重新渲染组件?

json - 无法获取嵌套 JSON 对象属性 Typescript

reactjs - 如何覆盖 Material-UI Popover 样式?

javascript - 在 React.js 中更改组件的父级

javascript - Sencha Touch 2 缩放面板内容

javascript - Jquery 点击菜单可以工作,但在 Safari 和大多数移动浏览器中完全透明

javascript - 如何以等距方式在曲线上使用setLineDash?

javascript - 流: Dynamically generate string union types?

javascript - React-倒计时-现在不在渲染上更新