![](/img/trans.png)
[英]Why does Angular say “Uncaught ReferenceError: $scope is not defined”?
[英]Why does it say uncaught Reference error: counter is not defined?
不幸的是,我正在练习如何使用reactJS创建具有状态的组件。 我已经从前面的示例中得到了镜像,除了reandomNumber函数,每次单击按钮都会生成一个新数字,实际上没有什么区别。 如果有人可以给我有关我做错了什么的反馈,我将不胜感激。 另外,我的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>
);
}
});
您在此处使用了错误的标点符号,并意外地为0
而不是带有属性的对象添加了带标签的语句:
getInitialState: function() {
return(
counter: 0
);
},
这应该是
getInitialState: function() {
return {
counter: 0
};
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.