I need to make two separate api calls in the same react component. However, I'm having trouble getting information when I try to use it in my HTML. Specifically the similar data in this code.
function Info() {
let params = useParams();
const [details, setDetails] = useState({});
const [similar, setSimilar] = useState({});
const [activeTab, setActiveTab] = useState('instructions')
const fetchDetails = async () => {
const data = await fetch(`https://api.spoonacular.com/recipes/${params.name}/information?apiKey=${process.env.REACT_APP_API_KEY}`)
const detailData = await data.json();
setDetails(detailData);
console.log(detailData);
};
const fetchSimilar = async () => {
const data = await fetch(`https://api.spoonacular.com/recipes/${params.name}/similar?apiKey=${process.env.REACT_APP_API_KEY}`)
const similarData = await data.json();
setSimilar(similarData);
console.log(similarData);
};
useEffect(() => {
fetchDetails();
fetchSimilar();
}, [params.name]);
return (
<DetailWrapper>
<div>
<h2>{details.title}</h2>
<img src={details.image} alt={details.title} />
</div>
<InfoDiv>
<Button className={activeTab === 'instructions' ? 'active' : ''} onClick={() => setActiveTab('instructions')}>Instructions</Button>
<Button className={activeTab === 'ingredients' ? 'active' : ''} onClick={() => setActiveTab('ingredients')}>Ingredients</Button>
{activeTab === 'instructions' && (
<div>
<p dangerouslySetInnerHTML={{__html: details.summary}}></p>
<p dangerouslySetInnerHTML={{__html: details.instructions}}></p>
</div>
)}
{activeTab === 'ingredients' && (
<ul>
{details.extendedIngredients.map((ingredients) => (
<li key={ingredients.id}>{ingredients.original}</li>
))}
</ul>
)}
</InfoDiv>
<div>
<p>{similar.title}</p>
</div>
</DetailWrapper>
)
}
ferent way I should be making multiple calls in one file? Should I fetch both url data in one function instead of two?
I already tried to make a separate component which holds the data I need however I still got the same results. This leads me to believe that its just how I'm calling this request.