Aas*_*Otp 1 javascript arrays javascript-objects reactjs map-function
我有一个数组时间状态并且里面有数据。我想要做的是通过这个状态映射并使用道具调用子组件
我的状态
this.state = {
user:'',
feedArray:[],
}
Run Code Online (Sandbox Code Playgroud)
数据设定功能
//I CALLED THIS IN COMPONENTDIDMOUNT
renderFeed(){
rdb.ref("feeds").once('value',(snapshot)=>{
snapshot.forEach((childSnapshot)=>{
this.setState({feedArray:Object.values(childSnapshot.val())})
})
}).then(()=>{
console.log(this.state.feedArray);
})
Run Code Online (Sandbox Code Playgroud)
}
退货部分
render() {
if (this.state.feedArray) {
this.state.feedArray.map((feed,id)=>{
console.log(feed.body); //this works fine
return (<FeedElement id={feed.id} body={feed.body}/> ); //This Not Works
})
}
}
Run Code Online (Sandbox Code Playgroud)
这是在 console.log(this.state.feedArray) 上 Cosoled 的日志
(4) […]
?
0: Object { author: "AashiqOtp", body: "kkk", feedid: "-M1_6POMRyqRv2tIKrF9", … }
?
1: Object { author: "AashiqOtp", body: "kkk", feedid: "-M1_6XYaUAlsXlwnAbcp", … }
?
2: Object { author: "AashiqOtp", body: "f", feedid: "-M1_HB07eh_08OFFRBbO", … }
?
3: Object { author: "AashiqOtp", body: "we have a new rm", feedid: "-M1d4xwLmSUKA0RZlH-Q", … }
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?提前致谢
您可以通过 feed.feedid 而不是 feed.id 并在 map 函数之前返回。
render() {
if (this.state.feedArray) {
return this.state.feedArray.map((feed,id)=>
(<FeedElement id={feed.id} body={feed.body}/> ); //This Not Works
)
}else {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
希望它应该工作