简体   繁体   English

为什么说未捕获参考错误:未定义计数器?

[英]Why does it say uncaught Reference error: counter is not defined?

I'm practicing how to create components that have a state with reactJS unfortunately I can't figure out what is wrong. 不幸的是,我正在练习如何使用reactJS创建具有状态的组件。 I have mirrored this from a previous example and there is literally no difference aside from the reandomNumber function in which I generate a new number each time I click the button. 我已经从前面的示例中得到了镜像,除了reandomNumber函数,每次单击按钮都会生成一个新数字,实际上没有什么区别。 If anybody can give me feedback on what I did wrong I would appreciate it. 如果有人可以给我有关我做错了什么的反馈,我将不胜感激。 Also, there is nothing showing within my UI window. 另外,我的UI窗口中没有任何显示。

var Count = React.createClass({
    render: function() {
        var counterStyle = {
            fontSize: 20,
            fontFamily: "times",
            padding: 50,
            color: "blue"
        };

        return(
            <p style="counterStyle">{this.props.display}</p>
        );
    }
});


var ButtonButton = React.createClass({
    render: function() {
        var buttonStyle = {
            fontSize: "1em",
            width: 60,
            height: 40,
            fontFamily: "times",
            color: "teal",
            fontWeight: "bold",
            lineHeight: "3px"
        };

        return(
            <button style={buttonStyle} onClick={this.props.eventHandle}>Random No.</button>
       );
    }
});

var ButtonSquare = React.createClass({
    getInitialState: function() {
        return(
            counter: 0
        );
    },

    randomNumber: function() {
        this.setState({
            counter: Math.floor(Math.random())
        });
    },


    render: function() {
        var linkStyle = {
            width: 900,
            height: 900,
            backgroundColor: "blue",
            textAlign: "center"
        };


        return (
            <div style={linkStyle}>
                <Count display={this.state.counter}/>
                <ButtonButton eventHandle={this.randomNumber}/>
            </div>
        );
    }
});

You've used the wrong punctuation here and accidently made a labelled statement for 0 instead of an object with a property: 您在此处使用了错误的标点符号,并意外地为0而不是带有属性的对象添加了带标签的语句:

getInitialState: function() {
    return(
        counter: 0
    );
},

This should be 这应该是

getInitialState: function() {
    return {
        counter: 0
    };
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM