【发布时间】:2018-11-18 22:29:02
【问题描述】:
我对 React 还是很陌生。我在服务器上有 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>
);
}
}
【问题讨论】:
-
硬编码索引看起来很糟糕,为什么不添加一些数据来指示相关元素的标签。另请注意,您的 selectedProfile 将在首次加载时为空,getitems 应该位于
componentWillMount()生命周期事件、XmlHttpRequest vs fetch 或 axios 或类似的库中,就问题而言,输出 jsondata 会很有帮助(例如为什么jsondata中的第一项) -
@Icepickle 仍未定义
-
@Icepickle jsonData: 21 个这样的单元:
0 :{general: {…}, job: {…}, contact: {…}, address: {…}}
标签: javascript reactjs jsx semantic-ui-react