I'm trying to iterate over an array of Objects in React fetched from Django. My object is an array, but when I try to map it I get a typeerror: cannot read property map of undefined.
The state has structure:
Unit: { alliances: [{description: "", id: 6, name: "Elusive"}, {...}] icon_url id name }
which is what shows in React Developer tools as well. In another component I'm doing something very similar with a state that is a nested array of units with the same structure as the above, so I'm at a loss as to why this doesn't work.
class UnitInfo extends Component { state = { unit: [] }; // ... render() { return ( <div> <h1>{this.state.unit.name}</h1> <img src={this.state.unit.icon_url} alt="{this.state.unit.name} icon" /> {this.state.unit.alliances.map(alliance => ( <div key={alliance.id}> <h4>{alliance.name}</h4> </div> ))} </div> ); } }
statewith aunitthat containsalliances.