【发布时间】:2018-02-25 21:00:07
【问题描述】:
帖子:
[
{ id:1, name:'post1', desc: 'post1 desc', parentId: 1 },
{ id:2, name:'post2', desc: 'post2 desc', parentId: 2 },
{ id:3, name:'post3', desc: 'post3 desc', parentId: 1 }
]
来自 reducer 的帖子。有mapStateToProps,可以在控制台打印this.props.posts。
我的功能:
getPosts = (idPost = 1) =>{
const { posts } = this.props.posts
return
posts.filter(id => posts[id].parentId == idPost)
.map((key, idx) => {
const myPost = posts[key];
return(
<View>
<Text>
{ myPost.name }
</Text>
</View>
)
});
};
错误:Requested keys of a value that is not an object. 函数的第 1 行。
我做错了什么?
更新 1
getPostsNew = (namePost = 'post1) =>{
const { posts } = this.props.posts
return posts.map((key, idx) => {
if (key.name == namePost) {
return(
<View>
<Text>
{ key.name }
</Text>
</View>
)
} else {
return null
}
});
};
【问题讨论】:
-
不应该是 const { posts } = this.props;而不是 this.props.posts?此外,posts.filter 会在回调中为您提供每个帖子,而不仅仅是 id。还要检查您的过滤器功能。地图功能也是如此
标签: reactjs react-native ecmascript-6 react-redux