【发布时间】:2017-04-05 08:26:58
【问题描述】:
我的 react-redux 应用程序在 JSON 中获得了一条记录,但该记录是一个数组,因此它看起来像这样(注意 [] 括号):
{"person":[{"PersonID":1,"Name":"John Smith","Gender":0}]}
因此,redux 商店将其显示为 person->0->{"PersonID":1,"Name":"John Smith","Gender":0}。因此,状态显示 person 对象为空:
Name: this.props.person?this.props.person.Name:'object is empty',
我的 PersonPage.js 包含这样的详细信息页面:
<PersonDetail person={this.props.person} />
详情页有这个:
import React from 'react';
import classnames from 'classnames';
class PersonDetail extends React.Component {
state = {
Name: this.props.person?this.props.person.Name:'',
PersonID: this.props.person?this.props.person.PersonID:null,
loading: false,
done: false
}
componentWillReceiveProps = (nextProps) => {
this.setState({
PersonID: nextProps.person.PersonID,
Name: nextProps.person.Name
});
}
这是我的原始 Redux 状态:
people: [
[
{
PersonID: 51,
Name: 'John Smith',
Gender: 0
}
]
]
【问题讨论】:
-
您想要类似人的东西:[ {}, {}, {} ]?或者只是从那里获取对象?
-
我已经有“人”(这适用于列表页面),但对于单条记录,我只想要“人”。但是我可以通过采用 0 索引来让它工作,我也很好。它只是看起来不那么干净或合乎逻辑。
-
检查我的答案,我认为编写一个 util 函数来解析“人”是有意义的