Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法读取属性';设置状态';未定义的_Javascript_Reactjs_Webpack_Ecmascript 6_Babeljs - Fatal编程技术网

Javascript 无法读取属性';设置状态';未定义的

Javascript 无法读取属性';设置状态';未定义的,javascript,reactjs,webpack,ecmascript-6,babeljs,Javascript,Reactjs,Webpack,Ecmascript 6,Babeljs,import React,{Component}来自'React'; 从“react dom”导入react dom; 从“axios”导入axios; 从“/components/listvideos”导入视频详细信息; 类YoutubeApp扩展组件{ 建造师(道具){ 超级(道具) 此.state={ 视频:[] }; 这是apiCall(“冲浪”); } apiCall(学期){ 常量参数={ 部分:'代码片段', 密钥:应用程序密钥, 问:任期, 键入:“视频” }; get(APP_

import React,{Component}来自'React';
从“react dom”导入react dom;
从“axios”导入axios;
从“/components/listvideos”导入视频详细信息;
类YoutubeApp扩展组件{
建造师(道具){
超级(道具)
此.state={
视频:[]
};
这是apiCall(“冲浪”);
}
apiCall(学期){
常量参数={
部分:'代码片段',
密钥:应用程序密钥,
问:任期,
键入:“视频”
};
get(APP_URL,{params:params})
.然后(功能(响应){
这是我的国家({
视频:回应
})
})
.catch(函数(错误){
控制台错误(error);
});
}
render(){
返回(
)
}
}
ReactDOM.render(
,
document.getElementById('app')
)

您正在承诺链中使用
函数()
,这将更改
的范围。如果您使用的是ES6,我建议您使用arrow函数来维护类的作用域。例如:

你正在使用

axios.get(APP_URL, { params: params})
    .then(function(response) { // this resets the scope
       this.setState({
           videos:response
       })
    })
尝试使用箭头功能:

axios.get(APP_URL, { params: params})
    .then((response) => {
       this.setState({
           videos:response
       })
    })

您正在承诺链中使用
function()
,这将更改
的范围。如果您使用的是ES6,我建议您使用arrow函数来维护类的作用域。例如:

你正在使用

axios.get(APP_URL, { params: params})
    .then(function(response) { // this resets the scope
       this.setState({
           videos:response
       })
    })
尝试使用箭头功能:

axios.get(APP_URL, { params: params})
    .then((response) => {
       this.setState({
           videos:response
       })
    })

您可以将当前上下文
分配给变量,也可以使用ES5或ES6保持一致

apiCall(term) {
  const params = {
    part: 'snippet',
    key: APP_KEY,
    q: term,
    type: 'video'
  };
  const _this = this;

  axios.get(APP_URL, { params: params})
   .then((response) => {
     _this.setState({
       videos: response
     });
   })
   .catch((error) => {
    console.error(error);
   });
}

您可以将当前上下文
分配给变量,也可以使用ES5或ES6保持一致

apiCall(term) {
  const params = {
    part: 'snippet',
    key: APP_KEY,
    q: term,
    type: 'video'
  };
  const _this = this;

  axios.get(APP_URL, { params: params})
   .then((response) => {
     _this.setState({
       videos: response
     });
   })
   .catch((error) => {
    console.error(error);
   });
}

这是由于API调用中的作用域。
的值在承诺链中的
响应
回调中不同。有两种方法可以解决这个问题

您可以在
apiCall
方法的开始处设置一个引用
this
的变量

apiCall(term){

    const that = this;

    const params = {
        part: 'snippet',
        key:APP_KEY,
        q:term,
        type: 'video'
    };

   axios.get(APP_URL, { params: params}).then(function(response) {
       that.setState({
           videos:response
       });
    }).catch(function(error) {
       console.error(error);
  });
}

或者,您可以使用ES6 arrow函数,这些函数保持原始范围,如下所示:

axios.get(APP_URL, { params: params}).then(response => {
    this.setState({
         videos:response
    });
})

这是由于API调用中的作用域。
的值在承诺链中的
响应
回调中不同。有两种方法可以解决这个问题

您可以在
apiCall
方法的开始处设置一个引用
this
的变量

apiCall(term){

    const that = this;

    const params = {
        part: 'snippet',
        key:APP_KEY,
        q:term,
        type: 'video'
    };

   axios.get(APP_URL, { params: params}).then(function(response) {
       that.setState({
           videos:response
       });
    }).catch(function(error) {
       console.error(error);
  });
}

或者,您可以使用ES6 arrow函数,这些函数保持原始范围,如下所示:

axios.get(APP_URL, { params: params}).then(response => {
    this.setState({
         videos:response
    });
})

您可能还需要在
this.apiCall('surfing')之前添加
this.apiCall=this.apiCall.bind(this)
构造函数
方法上执行code>。这将把
apiCall
的范围绑定到react组件的范围

详情如下:


另外,删除
this.apiCall(“冲浪”)
constructor
中将其放入
componentdiddmount
函数中,构造函数不应用于执行。

您还可能需要在
this.apiCall=this.apiCall.bind(this)
之前添加
this.apiCall('surning')构造函数
方法上执行code>。这将把
apiCall
的范围绑定到react组件的范围

详情如下:


另外,删除
this.apiCall(“冲浪”)构造函数中选择code>并将其放入
componentDidMount
函数中,构造函数不应用于执行。

绑定问题使用箭头函数:
。然后((响应)=>{
不重复。不同的东西。这个问题的答案是关于ES6不一定反应。绑定问题使用箭头函数:
。然后((反应)=>{
不重复。不同的东西。这个问题的答案是关于ES6不一定反应。