How to pass variable from a component’s function to render (REACTJS)

  javascript, reactjs, variables

I’m sending a request to the API and I’m trying to pass a variable from that fetch to my render but I don’t seem to understand how to do it. I want to pass the variable pic to render

        fetch(`${url}/photo/${username}`, {
        method: "GET",
        headers: {"Content-Type": "application/json"}
    }).then(function (response) {
        return response.json();
    })
        .then(function (myJson) {
            let imgOutput = {}
            for (let i = 0; i < myJson.length; i++) {
                imgOutput[i] = `data:image/${myJson[i].format};base64,${myJson[i].pictureBytes}`
            }
            let pic = JSON.stringify(imgOutput)
            this.setState({ pic: pic})
        }).catch((error) => {
        alert(error);
    })

Source: Ask Javascript Questions

LEAVE A COMMENT