我不明白在一個React應用程序中的香草JavaScript和ES6之間的語法差異。 我不工作的第一代碼是setState函數在使用ES6語法的React中未定義?
class App extends Component{
constructor(props){
super(props);
this.state = {videos:[]};
YTSearch({key: API_KEY,term :'surfboards'},function(videos){
this.setState({videos : videos});
});
}
這給在控制檯
錯誤「未定義的‘一’不能讀取屬性」的setState但改變語法
YTSearch({key: API_KEY,term :'surfboards'},(videos)=>{
this.setState({videos : videos});
});
解決了這個問題。是不是都一樣的東西(我可能是錯的)。採用
function(videos){}
和
(videos) => {}
我不舒服的javascript,所以任何幫助表示讚賞。
的可能的複製[Arrow功能VS函數聲明/表情:他們當量/更換?](https://開頭計算器。 com/questions/34361379/arrow-function-vs-function-declaration-expressions-are-they-equivalent-exch) – 4castle