我在我的代码中的函数中使用setState()。我正在将对象传递给该函数。问题是代码根本没有进入函数updateState
。在我的代码中,请搜索alert("got into the function")
并尝试理解编译器无法访问该警报代码行的原因。
感谢
var React = require('react');
var Parse = require('parse').Parse;
var ParseReact = require('parse-react');
module.exports = React.createClass({
mixins: [ParseReact.Mixin],
//Observe Function - a newly proposed function for prarse react integration
observe: function() {
//declare any variable you need here.
return {
product: (new Parse.Query('product'))
.ascending('createdAt')
};
},
getInitialState: function() {
return {
// if the cookie is there, It will return the cookie value which I saved as 'true' word.
FTWVeyCc4o : false,
};
},
//Render
render: function() {
if(this.data.product.length){
var content = (
<div >
{this.data.product.map(function(p) {
this.updateState.bind(this,p);
return (
//From Design
<div className="col-md-4">
<div className="item text-center">
<div className="photo">
<img src={ p.productImgUrl } />
<br /><br />
<h5>{p.name} <br /> {p.size}</h5>
</div>
</div>
<div className="action">
<button
type="button"
className={"btn btn-"
+ (
(this.state.FTWVeyCc4o && (p.objectId == "FTWVeyCc4o")) || (this.state.aJ3DoJp352 && (p.objectId == "aJ3DoJp352")) || (this.state.dgeRhSG21U && (p.objectId == "dgeRhSG21U")) ? 'warning' : 'primary'
)}
onClick={this.ProductButtonClicked.bind(this, p)}
>
{(
(this.state.FTWVeyCc4o && (p.objectId == "FTWVeyCc4o")) || (this.state.aJ3DoJp352 && (p.objectId == "aJ3DoJp352")) || (this.state.dgeRhSG21U && (p.objectId == "dgeRhSG21U")) ? 'Remove' : 'Add'
)}
</button>
</div>
</div>
);
}, this)}
</div>
);
}
else{
var content = (<div>
</div>)
}
return content;
},
//update state
updateState:function(s){
alert("got into the function")
if(s.objectId == "FTWVeyCc4o")
{
this.setState({FTWVeyCc4o: this.getCookiefunc('RahlTradingProductID-FTWVeyCc4o') });
}
},
ProductButtonClicked:function(obj){
if( this.getCookiefunc('RahlTradingProductID-' + obj.objectId) == "" )
{//alert("Create Cookie")
//create the cookie
document.cookie="RahlTradingProductID-" + obj.objectId + "=true; expires=Thu, 18 Dec 2017 12:00:00 UTC; path=/";
//update state
if(obj.objectId == "FTWVeyCc4o")
{
this.setState({FTWVeyCc4o:true});
}
else if(obj.objectId == "aJ3DoJp352")
{
this.setState({aJ3DoJp352:true});
}
else if(obj.objectId == "dgeRhSG21U")
{
this.setState({dgeRhSG21U:true});
}
}
else
{//alert("Delete Cookie")
//delete the cookie
document.cookie="RahlTradingProductID-" + obj.objectId + "=; expires=Thu, 18 Dec 2000 12:00:00 UTC; path=/";
if(obj.objectId == "FTWVeyCc4o")
{
this.setState({FTWVeyCc4o:false});
}
else if(obj.objectId == "aJ3DoJp352")
{
this.setState({aJ3DoJp352:false});
}
else if(obj.objectId == "dgeRhSG21U")
{
this.setState({dgeRhSG21U:false});
}
}
/
getCookiefunc:function(cname){
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
});
代码如下:
答案 0 :(得分:0)
看起来你的函数updateState()
实际上从未从你的代码中调用过。
this.updateState.bind(this,p)
绑定您的函数的参数,但不会调用它。
您的代码需要:
this.updateState(p)
声明,onClick = { this.updateState.bind(this,p)
}
在第二种情况下,只要用户单击按钮,就会调用您的函数。
目前,按钮点击调用productButtonClicked
。但是在该函数中,您只调用setState,而不是updateState。