【发布时间】:2019-09-08 13:45:11
【问题描述】:
我已经读过this question 和this one,应该更适合我的需要。
但是,我还是不明白。
我有这个代码
class DivRatingOverall extends React.Component {
constructor(props) {
super(props);
let overallRatingMeta = wp.data.select('core/editor').getCurrentPost().meta.overall_rating;
this.state = {overallRating: overallRatingMeta};
this.printDivOverallRater = this.printDivOverallRater.bind(this);
}
printDivOverallRater() {
return (
<div id="overall-rater-panel" ref={()=>
raterJs({
starSize: 32,
step: 0.1,
showToolTip: false,
rating: this.state.overallRating,
readOnly: false,
element: document.querySelector("#overall-rater-panel"),
rateCallback: function rateCallback(rating, done) {
rating = rating.toFixed(1);
rating = parseFloat(rating);
this.setRating(rating);
wp.data.dispatch('core/editor').editPost(
{ meta: { overall_rating: rating } }
);
//I can't access setState here
//this.setState({overallRating: rating});
done();
}
})
}
/>
)
}
render () {
return (
<div>
{this.OverallRateThis}
<div>
{this.printDivOverallRater()}
</div>
</div>
);
}
但是,在回调函数中,我无法访问this.setState,因为this现在引用了raterJS,an exteranl js library that I use
如何在回调中更改状态?
【问题讨论】:
标签: reactjs callback this bind