When the main component loads, I send a GET request to my API to get all the objects that have certain value in a field called "ship_name"
, this means more "builds" might arrive, so there will be more to be rendered. After loading the main Component, I have created a second component in the same file, called "Builds". As props, I send to this component this.state.builds, an array of objects.
Better explanation:
- User inserts "ship_name" in a search bar
- API looks for all builds that have that "ship_name"
- All the builds are sent to the sub component
- Using the "gears" (Gear Names) API looks for the file name and sends it back
- ! React renders before the links arrive
<Builds builds={this.state.builds} />
The structure of "builds" is like this
[
{
"gears": [
"Triple 410mm (10th Year Type Prototype)",
"Triple 155mm (3rd Year Type)",
"Twin 40mm Bofors STAAG Mk II",
"High Performance Fire Control Radar",
"Type 1 Armor Piercing Shell"
],
"rarities": [
"o",
"o",
"o",
"o",
"o"
],
"_id": "5eac7b450e64096b082f4c43",
"ship_name": "Gascogne",
"pv": "e"
},
{
"gears": [
"Triple 410mm (10th Year Type Prototype)",
"Triple 155mm (3rd Year Type)",
"Twin 40mm Bofors STAAG Mk II",
"High Performance Fire Control Radar",
"Type 1 Armor Piercing Shell"
],
"rarities": [
"o",
"o",
"o",
"o",
"o"
],
"_id": "5f131d6e2e3f16ef3de48e4f",
"ship_name": "Gascogne",
"pv": "e"
}
]
Everything works till this point.
render() {
return (
this.props.builds.map((build) => (
<div className="container" key={uuidv4()}>
<img src={"/img/gears/" + this.getGearData(build.gears[0])} className={build.rarities[0] + " gI g1"}></img><div className={"t t1"}>{build.gears[0]}</div>
<img src={"/img/gears/" + this.getGearData(build.gears[1])} className={build.rarities[1] + " gI g2"}></img><div className={"t t2"}>{build.gears[1]}</div>
<img src={"/img/gears/" + this.getGearData(build.gears[2])} className={build.rarities[2] + " gI g3"}></img><div className={"t t3"}>{build.gears[2]}</div>
<img src={"/img/gears/" + this.getGearData(build.gears[3])} className={build.rarities[3] + " gI g4"}></img><div className={"t t4"}>{build.gears[3]}</div>
<img src={"/img/gears/" + this.getGearData(build.gears[4])} className={build.rarities[4] + " gI g5"}></img><div className={"t t5"}>{build.gears[4]}</div>
</div>
))
)
}
/*------------------------------*/
getGearData(gear_name) {
let requestString = encodeURI('http://localhost:5000/gears/one?q=' + gear_name)
let data;
Axios.get(requestString)
.then(res => {
console.log(res.data[0].image_link)
data = res.data[0].image_link
})
return data
/* what */
}
What happens here is that while I am rendering, at the same time I am trying to get the URL of the images. The GET methods works, in fact the console.log prints the actual name of the file
32200.png gear.component.js:76
26600.png gear.component.js:76
1260.png gear.component.js:76
600.png gear.component.js:76
34180.png gear.component.js:76
but on the SRC the result is "/img/gears/undefined". One thing that I can't understand is why all the results are printed 2 times, but that's not much of a problem since in the end the things get rendered only once correctly.
I have tried to use many methods, state, Promises (even if I haven't really understood how those work), it's the first time I'm really stuck like this. The fact is that the main component uses the "ship_name" to get the file name of the "ship_icon" and the "faction_icon" and it gives no problem, probably because I use the state there.
Here's an idea of how it looks like right now
And here's an idea of how it should look like
I completed this personal project once using php and SQL, now I am trying to do it with React and MongoDB.