我一直在使用unsplash api来制作图像搜索网站。在安装网站时,图像显示良好。但是,当我输入搜索查询并单击提交时,我得到data.map is not a function
。
我希望能够显示搜索结果,但是我认为我在handleSubmit
函数中做错了,其中对搜索查询的api调用已完成。帮助将不胜感激
import React, { Component } from "react";
import Pagination from "./Pagination";
import List from "./List";
import "./styles.css";
import axios from "axios";
const LOAD_STATE = {
SUCCESS: "SUCCESS",
ERROR: "ERROR",
LOADING: "LOADING"
};
const BASE_URL = "https://api.unsplash.com/photos?page=1";
export default class App extends Component {
constructor() {
super();
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.state = {
photos: [],
totalPhotos: 0,
perPage: 5,
currentPage: 1,
loadState: LOAD_STATE.LOADING,
search: ""
};
}
handleChange(e) {
this.setState({ search: e.target.value });
}
handleSubmit() {
const { search, perPage } = this.state;
console.log(search);
const url =
"https://api.unsplash.com/search/photos?page=1&query=" +
search +
"&client_id=" +
appId;
const options = {
params: {
client_id: appId,
page: 1,
per_page: perPage,
order_by: "popularity"
}
};
this.setState({ loadState: LOAD_STATE.LOADING });
axios
.get(url, options)
.then(response => {
this.setState({
photos: response.data,
totalPhotos: parseInt(response.headers["x-total"]),
currentPage: 1,
loadState: LOAD_STATE.SUCCESS
});
})
.catch(() => {
this.setState({ loadState: LOAD_STATE.ERROR });
});
}
componentDidMount() {
this.fetchPhotos(this.state.currentPage);
}
fetchPhotos(page) {
var self = this;
const { perPage } = this.state;
const options = {
params: {
client_id: appId,
page: page,
per_page: perPage,
order_by: "popularity"
}
};
this.setState({ loadState: LOAD_STATE.LOADING });
axios
.get(BASE_URL, options)
.then(response => {
self.setState({
photos: response.data,
totalPhotos: parseInt(response.headers["x-total"]),
currentPage: page,
loadState: LOAD_STATE.SUCCESS
});
})
.catch(() => {
this.setState({ loadState: LOAD_STATE.ERROR });
});
}
render() {
return (
<div className="app">
<input
onChange={this.handleChange}
type="text"
name="search"
placeholder="Enter query"
/>
<button type="submit" onClick={this.handleSubmit} className="button">
Submit
</button>
<Pagination
current={this.state.currentPage}
total={this.state.totalPhotos}
perPage={this.state.perPage}
onPageChanged={this.fetchPhotos.bind(this)}
/>
{this.state.loadState === LOAD_STATE.LOADING ? (
<div className="loader" />
) : (
<List data={this.state.photos} />
)}
</div>
);
}
}
import React from "react";
import ListItem from "./ListItem";
const List = ({ data }) => {
var items = data.map(photo => <ListItem key={photo.id} photo={photo} />); //error in this line
return <div className="grid">{items}</div>;
};
export default List;
答案 0 :(得分:2)
您应该使用
axios
.get(url, options)
.then(response => {
const photos = response.data ? response.data.results : [];
this.setState({
photos,
totalPhotos: parseInt(response.headers["x-total"]),
currentPage: 1,
loadState: LOAD_STATE.SUCCESS
});
})
代替
axios
.get(url, options)
.then(response => {
this.setState({
photos: response.data,
totalPhotos: parseInt(response.headers["x-total"]),
currentPage: 1,
loadState: LOAD_STATE.SUCCESS
});
})
在handleSubmit函数中