【问题标题】:Use this in a callback [duplicate]在回调中使用它[重复]
【发布时间】:2019-09-08 13:45:11
【问题描述】:

我已经读过this questionthis 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


    【解决方案1】:

    使用箭头函数将其保存在更高范围内

    // arrow function preserve 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: (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();
                }
            })
        }
        />
    )
    }
    
    
    
    //save it in higher scope
    printDivOverallRater() {
        const self = this;
        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(rating, done){
    
                    rating = rating.toFixed(1);
                    rating = parseFloat(rating);
                    self.setRating(rating);
    
                    wp.data.dispatch('core/editor').editPost(
                        { meta: { overall_rating: rating } }
                    );
    
                    I can't access setState here
                    //this.setState({overallRating: rating});
    
                    done();
                }
            })
        }
        />
    )
    }
    

    【讨论】:

      猜你喜欢
      • 2016-07-29
      • 1970-01-01
      • 2018-09-29
      • 2021-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-12
      相关资源
      最近更新 更多