D
D
Daniel Chistyakov2021-01-20 14:21:37
React
Daniel Chistyakov, 2021-01-20 14:21:37

How to display variables from componentDidMount?

How do I display artistname and title in the second componentDidMount? Because now the value is not passed.

async componentDidMount() {
        const url = "https://api.laut.fm/station/city/current_song";
        const response = await fetch(url);
        const data = await response.json();
        this.setState({ info: data });
        const artistname = this.state.info.artist.name;
        const title = this.state.info.title;
    }
    async componentDidMount() {
        const image = "http://ws.audioscrobbler.com/2.0/?method=album.getInfo&artist=" + artistname + "&album=" + title + "&api_key=SECRET_KEY&format=json";
        const images = await fetch(image);
        const dataimage = await images.json();
        this.setState({ cover: dataimage });
        console.log(dataimage);
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2021-01-20
@danielchistyakov

state does not change immediately after this.setState, if you want to get new values, then

const data = await response.json();
this.setState({ info: data });
const artistname = data.artist.name;
const title = data.info.title;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question