I am quite new, this is my first question post I was hoping someone else will of had a similar problem.
I am receiving api data and turning into the JSON format. From that I can access the keys & values within an object (each monster)
hit_points: 17 (key: value)
hit_dice: "5d6" (key: value)
speed: {fly: 40, walk: 20} (object)
special_abilities: Array(1)
0:
desc: "some text"
name: "One with Wind"
I am able to access special_abilities[0].name, in this case i get back "One with Wind".
What I cannot work out how to do is when I get more then one ability how to display all 3 without crashing the script when a monster has only 1 ability.
import React, {useState, useEffect} from 'react';
import './App.css';
function currentMonster({match}) {
const [item, setItem] = useState([]);
useEffect (() => {
fetchMonster();
}, []);
const fetchMonster = async () => {
const mob = match.params.id.toLowerCase();
const fetchMonster = await fetch(`https://api.open5e.com/monsters/${mob}/?format=json`)
const monster = await fetchMonster.json();
setItem(monster)
console.log(monster);
// const abilities = monster.special_abilities[0].name
}
return (
<div>
<h1>{item.name}</h1>
<p>Size: {item.size}</p>
<p>Race: {item.type}</p>
<p>Alignment: {item.alignment}</p>
<p>Armor Class: {item.armor_class}</p>
<p>Hit points: {item.hit_points}</p>
<p>Hit dice: {item.hit_dice}</p>
<p>Strength: {item.strength}</p>
<p>Dexterity: {item.dexterity}</p>
<p>Constitution: {item.constitution}</p>
<p>Intelligence: {item.intelligence}</p>
<p>Wisdom: {item.wisdom}</p>
<p>Charisma: {item.charisma}</p>
<p>Senses: {item.senses}</p>
<p>CR: {item.challenge_rating}</p>
</div>
)
}
export default currentMonster;
This is the code I have currently, I wish to add another <p></p> on the list displaying each special ability and desc of the monsters, and when they have more then one ability those also.
Before asking I have tried to:
const [ability, setAbility] = useState([]);
setAbility(monster.special_abilities[0])
and then <p>{ability.name}</p>
while this works for one monster, if the monster has more then one ability I cannot see them. If the monster has less then two abilities and I log say [0][1][2] the website crashes.
I hope someone can help me with this while questions similar to this have been asked before in stack overflow I have not been able to find inspiration or an answer.
Kind regards,
Sheep
s not true the API link was just to see what API i was using i guess i caused confusion, i mean to say I am searching for just one monster by replacing the URL component with the monsters name: https://api.open5e.com/monsters/${mob}/?format=json) so each time the api runs it is getting just one "item" or monster