learning from some tutorial docs, I'm trying to update the counter (increment / reset) depending on the condition, below is the actual code on the tutorial
var CowClicker = React.createClass({
getInitialState: function() {
return {
clicks: 10,
val:1 // this has been added by me
};
},
onCowClick: function(evt) {
this.setState({
clicks: this.state.clicks + 1,
val: this.state.val * this.state.clicks, // code part in question
}
});
},
render: function() {
return (
<div>
<div>Clicks: {this.state.clicks}</div>
<div>Val: {this.state.val}</div>
<img
src="http://s3.bypaulshen.com/buildwithreact/cow.png"
onClick={this.onCowClick}
className="cow"
/>
<p>Click the cow</p>
</div>
);
}
});
ReactDOM.render(
<CowClicker />,
document.getElementById('container')
);
Now, I'm trying to put condition based reset as below but it won't work :
onCowClick: function(evt) {
this.setState({
clicks: this.state.clicks + 1,
alert(this.state.val),
if(this.state.val > 1000000){ //this is not working
val: 1,
}else{
val: this.state.val * this.state.clicks,
}
});
},
This question might be way too obvious or stupid that's why I'm not getting a pointer on web as to where I'm wrong?
Can setState
not operate on logical operators? Is it purely to set stuffs or play around with using function calls (e.g. links searched 1 | 2)?