Trying a simple code to map over an array and, for each result, search an API for that result and spit out data retrieved from the API.
import React from 'react';
const App = () => {
let artistData = require('./mass-artists.json')
artistData.map(
(theArtist) => {
fetch(`[url]https://api.scryfall.com/cards/search?q=a:[/url]"${theArtist}"`)
.then((response) => {
return response.json();
})
.then((myJson) => {
console.log(JSON.stringify(myJson)); //returns the JSON data, so I know it's working
console.log(myJson.data[0].name); //returns the first card name
})
}
)
return(<span>This is here so React doesn't yell at me about no return statement.</span>)
}
export default App;
The JSON file is just a list of names of Magic artists. The map maps over the list of artists and searches on the API for them. I want to do the usual thing I do with the map, i.e. display something on the front-end. But I'm having trouble getting anything to work. I can log things to the console or do any Javascript function such as setting something's innerHTML, but where/how would I put a return statement to show the result in, say, a span tag? Even such as "Hello!".
Thanks as always!
useStateorsetState.Promise.allto batch requests, then deal with each request like that, or you could do it how you are and just set each response data in a unique part of the state so they don't overwrite, for example if you give them each a unique id. I recommend looking intoreduxto handle state, it's much easier, and for asynchronous actionsredux-thunkor the more advancedredux-saga.