反应回调绑定错误

时间:2018-04-02 15:58:55

标签: javascript reactjs

我在引号组件中有这段代码:

componentWillMount(){
    QuoteStore.on("newQuoteAdded",this.getQuotes);
}

componentWillUnmount(){
    QuoteStore.removeListener("newQuoteAdded",this.getQuotes);
}

getQuotes(){
    console.log(this);
    this.setState({
        quotes:QuoteStore.getQuotes()
    });
}

将新数据添加到磁通存储(QuoteStore)时会发出一个事件,如此

addQuote(quote,author){
    const newQuote=new Object();
    const date=Date.now().toString();
    newQuote.id=date;
    newQuote.author=author;
    newQuote.quote=quote;

    this.Quotes.push(newQuote);
    this.emit("newQuoteAdded");
}

当我的组件实际捕获此事件时,我收到此错误:

this.setState不是QuoteStore.getQuotes的函数

任何人都可以提供帮助,如何在事件捕获的 getQuotes()回调中将 this 绑定到Quotes组件?

0 个答案:

没有答案