Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.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
Reactjs React中的搜索功能和获取api_Reactjs_Fetch Api - Fatal编程技术网

Reactjs React中的搜索功能和获取api

Reactjs React中的搜索功能和获取api,reactjs,fetch-api,Reactjs,Fetch Api,我正在开发一个应用程序,它调用Unsplash的API并显示响应。当我在componentDidMount()中有fetch请求时,我能够通过/photos端点轻松获取/显示响应,但我想使应用程序可搜索,因此我添加了带有默认查询的performSearch() 但是添加performSearch会导致此错误: 这是我测试时得到的JSON: 我尝试过论坛上找到的其他解决方案,但到目前为止还没有解决问题。我肯定会得到一个数组,所以map不应该工作吗 类应用程序扩展组件{ 构造函数(){ 超级(

我正在开发一个应用程序,它调用Unsplash的API并显示响应。当我在
componentDidMount()
中有fetch请求时,我能够通过
/photos
端点轻松获取/显示响应,但我想使应用程序可搜索,因此我添加了带有默认查询的
performSearch()

但是添加
performSearch
会导致此错误:

这是我测试时得到的JSON:

我尝试过论坛上找到的其他解决方案,但到目前为止还没有解决问题。我肯定会得到一个数组,所以
map
不应该工作吗

类应用程序扩展组件{
构造函数(){
超级();
此.state={
结果:[],
加载:正确
};
}
componentDidMount(){
这是performSearch();
}
性能搜索=(查询='camping')=>{
取回(`https://api.unsplash.com/search/photos?page=3&query=${query}&client\u id=${client\u id}`)
.then(response=>response.json())
.然后(响应数据=>{
这是我的国家({
结果:应答数据,
加载:错误
});
})
.catch(错误=>{
log('Error fetching and parsing data',Error);
});
}
render(){
报税表(
{
(此.状态.加载)?加载

: } ); } } 导出默认应用程序


responseData.results是您正在寻找的数组。

将Plunker与您的示例一起使用会很有帮助并且更快。请尝试设置状态中的
responseData.results
。非常感谢您的帮助!这解决了问题。
performSearch = (query = 'camping') => {
    fetch(`https://api.unsplash.com/search/photos?page=3&query=${query}&client_id=${client_id}`)
      .then(response => response.json())
      .then(responseData => {
        this.setState({
          results: responseData.results,
          loading: false
        });
      })
      .catch(error => {
        console.log('Error fetching and parsing data', error);
      });
  }