React,如何访问类属性进入回调

时间:2017-08-10 10:03:48

标签: reactjs class properties callback access

我是React开发的新手......这是我写过的代码

constructor(props) {
    super(props);

    this.state = { videos: [] };
    YTSearch({ key: API_KEY, term: 'Led zeppelin'}, function(videos) {
        this.setState({ videos });
    });
}

但在运行时我将此错误发送到控制台

proxyConsole.js:56 TypeError: Cannot read property 'setState' of undefined
    at index.js:17
    at index.js:19
    at <anonymous>

&#34; this &#34;变量是 null ..为什么?我怎样才能访问this.setState ??

1 个答案:

答案 0 :(得分:1)

cell.baseview.isUserInteractionEnabled = true

试试这个