0

在遵循本指南( https://www.youtube.com/watch?v=6Ied4aZxUzc&t=1619s)时,我遇到了this.setState()总是返回的问题undefined

以下代码中的 IP 从我的json-server应用程序请求,并成功返回了一个承诺。但是 state 的值始终是undefined

componentDidMount() 
 const uri = 'http://192.168.1.220:3000/Songs';
      fetch(uri, {
                 method: 'GET',
                headers: {
                             "Content-Type": "application/json",
                            "Accept": "application/json"
                            },
     })
    .then((response) => {
               return response.json()
              })
    .then(json => {
               console.log(json);
               this.setState({songs: json});
    })
    .catch(function (error) {
               console.log('Looks like there was a problem: \n', error)
    });

我试图通过以下方式解决:关闭 cors,在外部调用静态方法componentDidMount(),并将状态传递给构造函数,使用.bind(this)on then 语句。

4

0 回答 0