【发布时间】:2017-11-13 14:33:01
【问题描述】:
因此,通过使用邮递员,我成功地调用了 yummly 的 url。我想呈现我的搜索结果。目前在第 29 - 36 行的渲染中,我有一个虚假的搜索,只是为了确保 Dom 可以正常工作,并且确实可以。如何从第 52 - 60 行获取 api 搜索以显示在 29 - 36 中。
import React from 'react';
import Request from 'superagent';
import _ from 'lodash';
export class Yum extends React.Component {
constructor(){
super();
this.state = {};
}
componentWillMount(){
/*var url = "http://api.yummly.com/v1/api/recipes?_app_id=5129dd16&_app_key=9772f1db10ba433223ad4e765dc2b537&=q"
Request.get(url).then((response) => {
console.log(response.body);
this.setState({
recipes: response.body.matches,
total: response.body.totalResults
});
});*/
this.search();
}
updateSearch(){
this.search(this.refs.query.value);
}
render(){
const title = 'Onion Soup';
const ingredients = ["onions", "beef stock"];
const listItems = ingredients.map((ingredient) => {
return (<h5>{ingredient}</h5>);
});
return(
<div>
<input ref="query" onChange = { (e) => {this.updateSearch();} } type="text" />
<h4>{title}</h4>
<ul>
<li>{listItems}</li>
</ul>
</div>
)
}
search(query = "onion") {
var url = `http://api.yummly.com/v1/api/recipes?_app_id=5129dd16&_app_key=9772f1db10ba433223ad4e765dc2b537&=q${query}&maxResult=1`
Request.get(url).then((response) => {
console.log(response.body.matches[0]);
console.log(query);
this.setState({
recipes: response.body.matches[0],
//total: response.body.totalResults
});
});
}
}
export default Yum;
【问题讨论】:
标签: reactjs superagent