【发布时间】:2018-02-26 03:07:55
【问题描述】:
api调用返回的数据为:
[
{
"1": {
"id_post": 1,
"id_user": "user01",
"text": "post1 text",
"parentId": 11
}
},
{
"2": {
"id_post": 2,
"id_user": "user01",
"text": "post2 text",
"parentId": 11
}
},
{
"3": {
"id_post": 3,
"id_user": "user01",
"text": "post3 text",
"parentId": 22
}
}
]
在 reducer 中,initialState 是:
const initialState = {
posts: [], // <= Have tried with posts: {} and posts: ''
isFetching: false,
error: {}
}
posts 的填充方式是:
case "FETCHING_POSTS_SUCCESS":
return {
...state,
isFetching: false,
posts: action.data
}
我可以mapStateToProps 发帖,如果我console.log("POSTS: ", this.props.posts) 我得到:
帖子:
0 : {1: {…}}
1 : {2: {…}}
2 : {3: {…}}
这就是以下功能不起作用的原因吗?:
renderposts = (parentId = 1) => {
return Object.keys(this.props.posts)
.filter(id_post => this.props.posts[id_post].parentId == parentId)
.map((key, idx) => {
const post = this.props.post[key];
return(
<View>
<Text> { post.id_post } - { post.text } </Text>
</View>
);
});
}
我没有收到任何错误,但也没有显示任何内容。
我在想 POSTS 是否返回为:
{1: {…}}
{2: {…}}
{3: {…}}
在控制台中(因此以这种方式呈现),它将起作用。如果是这样,我该怎么做?
非常感谢。
【问题讨论】:
-
如果
posts是一个数组,那么你为什么要使用Object.keys而不是.map? -
原因(1):我想为
parentId应用过滤器和(2):有另一个onPress函数以值的id为目标,所以对于第一篇文章,@ 987654337@ 是目标。
标签: arrays reactjs react-native ecmascript-6 react-redux