我不明白 React 应用程序中 vanilla javascript 和 ES6 之间的语法差异。我的第一个不起作用的代码是
class App extends Component{
constructor(props){
super(props);
this.state = {videos:[]};
YTSearch({key: API_KEY,term :'surfboards'},function(videos){
this.setState({videos : videos});
});
}
Run Code Online (Sandbox Code Playgroud)
这会在控制台中出现“无法读取未定义属性”的“setState”错误
但将语法更改为
YTSearch({key: API_KEY,term :'surfboards'},(videos)=>{
this.setState({videos : videos});
});
Run Code Online (Sandbox Code Playgroud)
解决问题。两者不是一回事(我可能错了)。使用
function(videos){}
Run Code Online (Sandbox Code Playgroud)
和
(videos) => {}
Run Code Online (Sandbox Code Playgroud)
我对 javascript 不满意,因此感谢您的任何帮助。