Using React.
I an app that takes data that has been submited by a search bar and returns a list of ids. I want to take those ids and pass it through another api. However, I'm not sure how to do this. I've looked at a few other pages on stackoverflow like this and this. The first one is a little too vague and the second one has independent api calls that run in sequence.
Here's my code:
import React from 'react'
import api1 from '../api/api1'
import SearchBar from './SearchBar'
class App extends React.Component {
state = { cards: [], new_cards: []};
onSearchSubmit = async (term) => {
const response = await api1.get("api/decks/" + term)
this.setState({ cards: response.data.data.cards, })
}
render () {
return (<div className='ui container'>
<SearchBar onSubmit={this.onSearchSubmit} />
{/* <CardList cards={this.state.cards} /> */}
</div>)
}
}
export default App
My code will take the first api and pass in the output ( a list of 36 items) down to the component CardList. However, the list isn't complete it needs to be a list of lists (you can tell I came over from python). I need to first make a call to a different api (one that is still local on my machine), grab the rest of the data and then pass in a new list (of 36 lists) to the component CardList. The second api works just fine by itself. I'm having trouble combining the two.
These are my thoughts, but it doesn't work. 1) create a new function getCardStats 2) use the map function to iterate over list one and run the api call. 3) append output of step 2) to a new list 4) return new list 5) pass in new list to CardList
There are 36 items in the first list and so the next api needs to be called 36 times. I don't know what the ramifications are of doing that many/little. Do we need to put something that waits for all 36 to finish or can we do them in parallel?
import React from 'react'
import api1 from '../api/api1'
import api2 from '../api/api2'
import SearchBar from './SearchBar'
class App extends React.Component {
state = { cards: [] };
onSearchSubmit = async (term) => {
const response = await api1.get("api/decks/" + term)
this.setState({ cards: response.data.. })
this.getCardStats(this.state.cards)
}
getCardStats = async (cards)=> {
console.log('we are here')
console.log(cards)
const response = props.cards.map(card => {
return await api2.get("api/cards/" + card)
})
}
console.log('finished')
console.log(response)
this.setState({ new_cards: response.data.data.cards })
render () {
return (<div className='ui container'>
<SearchBar onSubmit={this.onSearchSubmit} />
{/* <CardList cards={this.state.new_cards} /> */}
</div>)
}
}
export default App
Thoughts?