I started converting my code to TypeScript but for some reason i cannot get to work.
The code does not give an error but it does not show the list.
type song = {
id: number,
artist: string,
titre: string;
links: string[] | number,
has_youtube_link: number,
has_picture: number,
hour: number,
minutes: number,
votes: number
}
interface Songs {
listOfSongs: song[]
}
const LastPlayedSongs: React.FC = () => {
const [songs, setSong] = useState<Songs | null>(null)
useEffect(() => {
fetch("APILink")
.then(response => response.json())
.then(response => {
setSong(response);
})
.catch(err => {
console.log(err);
});
}, [])
return (
<>
<ul>
{songs?.listOfSongs?.map((song) => (
<li key={song.id}>{song.titre}, {song.id}</li>
))}
</ul>
</>
);
}
If i use the song type directly instead of the Songs interface it works.
type song = {
id: number,
artist: string,
titre: string;
links: string[] | number,
has_youtube_link: number,
has_picture: number,
hour: number,
minutes: number,
votes: number
}
const LastPlayedSongs: React.FC = () => {
const [songs, setSong] = useState<song[] | null>(null)
useEffect(() => {
fetch("APILink")
.then(response => response.json())
.then(response => {
setSong(response);
})
.catch(err => {
console.log(err);
});
}, [])
return (
<>
<ul>
{songs?.map((song) => (
<li key={song.id}>{song.titre}, {song.id}</li>
))}
</ul>
</>
);
}
Does anyone knows how to make the first one wor or give some informations on what I am doing wrong.
Kind regards
(14) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] 0: {id: 21520, artist: "Blondie", titre: "Rapture", links: "<a href="https://geo.itunes.apple.com/be/album/rap…:110px;height:40px;background-size:contain;"></a>", has_youtube_link: "0", …}