Używam API do pobierania danych. Kiedy ja się pocieszam.zarejestruj się moje dane, są one wyświetlane w postaci tablicy. Ale gdy próbuję nałożyć go na mapę, aby wyświetlić dane, on mi mówi, że .map - nie jest to funkcja. Stworzyłem własny hak useFetch, a następnie импортирую go w oddzielny komponent. Oto mój kod i zrzut ekranu konsoli.log:
useFetch.js
import { useEffect, useState } from 'react'
function useFetch(url) {
const [data, setData] = useState(null)
const [isLoading, setIsLoading] = useState(true)
const [error, setError] = useState(null)
useEffect(() => {
fetch(url)
.then(response => {
if (!response.ok) {
throw Error("Sorry, couldn't fetch data for this resource!")
}
return response.json()
})
.then(responseData => {
setData(responseData)
setIsLoading(false)
setError(null)
})
.catch(error => {
setIsLoading(false)
setError(error.message)
})
}, [url])
return { data, isLoading, error }
}
export default useFetch
List.js
import React from 'react'
import useFetch from './useFetch'
function PrizeList2017() {
const { data } = useFetch('http://api.nobelprize.org/v1/prize.json?year=2017&yearTo=2017')
return (
<div className="prize-list-2017-container">
<h1>2017</h1>
{data.map(prize => (
<div key={prize.id}>
<h2>{prize.category}</h2>
</div>
))}
{console.log(data)}
</div>
)
}
export default PrizeList2017
konsolę.magazyn
przystawka.obraz informacji o czasopiśmie
Jesteśmy bardzo wdzięczni za pomoc!