Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.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 使用react native获取数据错误_Javascript_Reactjs_React Native_React Redux - Fatal编程技术网

Javascript 使用react native获取数据错误

Javascript 使用react native获取数据错误,javascript,reactjs,react-native,react-redux,Javascript,Reactjs,React Native,React Redux,我用了两门课 我把第二节课叫做下面的课 let s = new SearchExtend(); output = s.fetchData(search) alert(output) 第二个功能: fetchData = (search) => { fetch('http://example.com/search.php' , { method: 'POST', headers: { 'Accept': 'applicat

我用了两门课 我把第二节课叫做下面的课

let s = new SearchExtend();

output = s.fetchData(search)
alert(output)
第二个功能:

fetchData = (search) => {

      fetch('http://example.com/search.php' , {
        method: 'POST',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({

          // Getting the search.
          search: search
        })
      }).then((response) => response.json())

       .then((responseJson) => {
             return responseJson;
       })
       .catch((error) => {
         console.error(error);
       });
}
但我在“警戒”中没有定义

使用this.setState而不是return时,会出现以下错误:


警告设置状态(…)只能更新已安装或正在安装的组件,因为您正在使用承诺,所以您将获得未定义的组件。最初它返回undefined,一旦promise被解析,您就得到了实际的数据

处理这一问题的更好方法是返还承诺,并通过以下方式从外部解决:-

const fetchApi = () => {
   return fetch("https://swapi.co/api/people" , {
    method: 'GET',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
    }
  }).then((response) => {
    return response.json();
  })
   .then((responseJson) => {
    return responseJson;
  })
    .catch((error) => {
    console.error(JSON.stringify(error));
  });
}


fetchApi()
.then((data) => {
  console.log("data", data);
})

设form=newformdata();s、 fetchData(form)使用FormData并通过append form传递参数和值。append(“paramname”,“paramvalue”)body:search不需要JSON.stringif由于在他的
fetchData
方法中没有返回任何内容,所以他正在未定义。它与承诺无关。感谢它的工作,如何在加载数据之前打印文本,如加载。。。或者导入类