gpt4 book ai didi

javascript - bind() : You are binding a component method to the component. React 会自动为你做这个吗?

转载 作者:数据小太阳 更新时间:2023-10-29 04:12:39 25 4
gpt4 key购买 nike

我从 reactJS.NET 收到这个警告

bind(): You are binding a component method to the component. React does this for you automatically in a high-performance way, so you can safely remove this call. See LikeCon

组件看起来像这样

var LikeCon = React.createClass({
handleClick: function() {
var data = new FormData();
var like = !this.state.like;
var likeCounter = this.state.likeCount;

data.append("catgoryType", this.state.categoryKey);
data.append("objectId", this.state.objectId);
data.append("like", like);

if(like)
likeCounter++;
else
likeCounter--;

this.setState({ like: like, likeCount: likeCounter, userId: this.state.userId, categoryKey: this.state.categoryKey, objectId: this.state.objectId});

var xhr = new XMLHttpRequest();
xhr.open("post", "http://localhost:2215/Home/SetLike", true);
xhr.onload = function() {
};
xhr.send(data);
},
getInitialState: function() {
return { like: this.props.initialLike, likeCount: this.props.initialLikeCount, userId: this.props.userId, categoryKey: this.props.categoryKey, objectId: this.props.objectId };
},
render(){
return this.renderLikeButton()
},
renderLikeButton(){
return (
content =
<div className="likeCon">
<div className={this.state.like==true ? "likeButConAct" : "likeButCon"}>
<div className="likeB" title={this.state.like==true ? "Unlike" : "Like"} onClick={this.handleClick.bind(this)} >
&nbsp;
</div>
{ this.state.likeCount > 0 ? <div className="likeCount">{this.state.likeCount}</div>: null}

</div>
</div>
);
}
})

我在调用 handleClick 方法时使用了一个绑定(bind),如果我删除它,我会得到另一个异常吗?那我该怎么办?

最佳答案

改为传递 *.bind(null,this)

参见 this Google Groups thread解释。

关于javascript - bind() : You are binding a component method to the component. React 会自动为你做这个吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28703820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com