1

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

4
  • Can you add the console.log of the repsonse? Commented Oct 16, 2020 at 14:21
  • @Domino987 it is a simple array like this : (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", …} Commented Oct 16, 2020 at 14:32
  • You should use capital letters for both types and interfaces. It makes it easier to distinguish between variables and types. Commented Oct 16, 2020 at 17:14
  • @LindaPaiste thank you for the advice, will change it and avoid doing it. Commented Oct 17, 2020 at 20:54

1 Answer 1

3

You should eighther change your interface or set the state correctly to the object attribute.

  1. type Songs = song [] then useState<Songs>([]) and then setSong(response)
  2. what you currently have as interface then setSong({listOfSongs:response})

preferably don't make your state nullable, it is an array and initially can be an empty array

Sign up to request clarification or add additional context in comments.

2 Comments

The two options do work, but which one would you recommend ? is one better than the other ?
Well if the state is supposed just to keep the response, we don't need to introduce a new attribute. So I will say the first approach is better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.