【发布时间】:2021-03-25 03:33:56
【问题描述】:
export class Diet extends Component {
constructor(props) {
super(props);
this.state = {
data: [],
};
}
updateSearch = (e) => {
axios
.get(
`https://api.spoonacular.com/food/products/search?apiKey{1234}&query=${data}&number=100`
)
.then((res) => {
this.setState({ data: res.data });
})
.catch((error) => {
console.log(error.response.data);
});
};
render() {
return (
<>
<SearchBar
placeholder="Search Food..."
onChangeText={this.updateSearch}
value={data}
/>
<List style={{ paddingTop: hp("2%") }}>
<TouchableOpacity>
{this.state.data.map(({ type }) => (
<Text>{this.state.type.products.title}</Text>
))}
</TouchableOpacity>
</List>
</>
);
}
}
大家好,我正在尝试构建一个SearchBar,当我输入SearchBar 时,URL 中的变量query 应该更改为我正在输入的内容,现在当我运行它时它会返回什么都没有。
数据库文档链接:https://spoonacular.com/food-api/docs#Search-Grocery-Products
【问题讨论】:
标签: reactjs react-native api search axios