反应,如何将对类属性的访问变成回调



我是 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>

">this"变量为..为什么?我怎样才能访问这个.setState ??

YTSearch({ key: API_KEY, term: 'Led zeppelin'}, (videos) => {
this.setState({ videos });
});

试试这个

最新更新