I'm quite new to React. I have JSON file on server, taking data from it via Node.JS. I want to assign the first part of this data to selectedProfile but it says undefined. When I try to assign it says Cannot read property '*AnyProp*' of undefined lower in JSX part of the code even if I try this.state.selectedProfile.general.firstName etc
import React from 'react';
import ReactDOM from 'react-dom';
import { Image, Search, Grid, Menu } from 'semantic-ui-react';
class ContentFeed extends React.Component {
constructor() {
super();
this.state = {
'items': [],
'prevName': '',
'i': 0,
'selectedProfile': []
}
this.getItems = this.getItems.bind(this);
}
componentWillMount() {
this.getItems();
}
getItems() {
var jsonData = [];
const xhr = new XMLHttpRequest();
xhr.onload = () => {
if (xhr.status === 200) {
jsonData = JSON.parse(xhr.responseText);
this.setState({'items': jsonData});
this.setState({'prevName': jsonData[0].general.firstName + ' ' + jsonData[0].general.lastName});
document.getElementById(this.state.prevName).style = 'border: 3px solid black';
this.setState({'selectedProfile': this.state.items[0]});
console.log(jsonData);
} else {
console.log('Error: No 200 OK response');
}
}
xhr.open('get', 'http://localhost:8000/api/item');
xhr.send();
}
handleItemClick = (e, { name }) => {
if (name !== this.state.prevName) {
document.getElementById(name).style = 'border: 3px solid black';
document.getElementById(this.state.prevName).style = 'border: 1px solid black';
this.setState({'prevName': name});
let i = this.state.items.findIndex(element => {
return (element.general.firstName + ' ' + element.general.lastName) === name;
});
this.setState({'i': i});
this.setState({'selectedProfile': this.state.items[i]});
}
}
render() {
return (
<Grid>
<Grid.Column width={4} style={{'height': '700px', 'overflowY': 'scroll'}}>
<Search />
{this.state.items.map( (item, index) => {
return (
<Menu key={index} fluid vertical tabular>
<Menu.Item
key={item.general.firstName + ' ' + item.general.lastName}
name={item.general.firstName + ' ' + item.general.lastName}
id={item.general.firstName + ' ' + item.general.lastName}
onClick={this.handleItemClick}
style={{border: '1px solid black'}}
>
<Image src={item.general.avatar} style={{'width': '50px', 'height': '50px'}}/>
<p><br />{item.general.firstName + ' ' + item.general.lastName}</p>
<p>{item.job.title}</p>
</Menu.Item>
</Menu>
)
})}
</Grid.Column>
<Grid.Column stretched width={12}>
<div style={{'margin': 'auto'}} id="content">
<h2 style={{'marginTop': '10px', 'overflow': 'auto'}}>{this.state.selectedProfile[0]} {this.state.selectedProfile[1]}</h2>
<p></p>
<Image style={{'margin': '10px', 'float': 'left'}} src={this.state.selectedProfile[2]}/>
<p>Company: {this.state.selectedProfile[3]}</p>
<p>Title: {this.state.selectedProfile[4]}</p><br />
<p>Email: {this.state.selectedProfile[5]}</p>
<p>Phone: {this.state.selectedProfile[6]}</p>
<p>Address: {this.state.selectedProfile[7]}</p>
<p>City: {this.state.selectedProfile[8]}</p>
<p>ZIP: {this.state.selectedProfile[9]}</p>
<p>Country: {this.state.selectedProfile[10]}</p>
</div>
</Grid.Column>
</Grid>
);
}
}
componentWillMount()lifecycle event, XmlHttpRequest vs fetch or axios or similar libarary, and as far as the question goes, having an output of your jsondata would be helpfull (eg why the first item in the jsondata)0 :{general: {…}, job: {…}, contact: {…}, address: {…}}