我在React中工作时,API中的数据出现了问题。我需要获取日期并打印搜索的所有结果,但我无法打印。目前数组的索引设置为[0],这允许我打印1个产品的所需详细信息。有什么想法可以打印所有结果吗?
const [drink, setDrink] = useState([])
const [drinkCat, setDrinkCat] = useState([])
const [drinkImg, setDrinkImg] = useState([])
const [drinkList, setDrinkList] = useState([])
const [id, setId] = useState()
const [idFromButtonClick, setIdFromButtonClick] = useState(1)
const [active, setActive] = useState("inactive")
const getDrink = () => {
Axios.get(`https://www.thecocktaildb.com/api/json/v1/1/search.php?s=${id}`).then((res)=> {
console.log(res)
setDrinkList(res.data.drinks.length)
setDrink(res.data.drinks[0].strDrink)
setDrinkCat(res.data.drinks[0].strCategory)
setDrinkImg(res.data.drinks[0].strDrinkThumb)
})
}
const handleClick = () => {
setIdFromButtonClick(id)
}
return (
<body>
<div>
<h1>
</h1>
<ul className="test">
<input id="searchBar" type="text" value={id} placeholder="Søg efter drink" onChange={e => setId(e.target.value)}></input>
<button onClick={handleClick, getDrink}>Søg🔍</button>
<li>
<Card style={{ width: '18rem' }}>
<Card.Img variant="top" src={drinkImg} />
<Card.Body>
<Card.Title>{drink}</Card.Title>
<Card.Text>
<p>{drinkCat}</p>
</Card.Text>
</Card.Body>
</Card>
</li>
</ul>
</div>
</body>
)}
2条答案
按热度按时间kuuvgm7e1#
只需运行一个
map
即可。nfzehxib2#
您应该Map您的列表:
...
...