我还是个新手。我在服务器上有一个JSON文件,通过Node.JS从其中获取数据。我想将数据的第一部分分配给selectedProfile
,但它显示为undefined
。当我尝试给它赋值时,即使我尝试this.state.selectedProfile.general.firstName
等,也会在代码的JSX部分中使用较低的Cannot read property '*AnyProp*' of undefined
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>
);
}
}
https://stackoverflow.com/questions/50767791
复制相似问题