将数组渲染为组件
Render array as component
如何在 React Native 中呈现来自我的 firestore 数据库的数据?
下面是我的代码,它只将数据推送到数组中,但我不知道如何在 Card 中使用它。我尝试使用 Flatlist 但失败了。
截至目前,每当我尝试在控制台中打印数据时,它看起来像这样:
Array [
"VD GOT JEALOUS",
"Sample Content",
]
我希望该数组在 Card 或 Text 组件中呈现
state = {
content: []
}
componentWillMount(){
/* similar to mounted in vue */
this.retrieveAnnouncement()
}
retrieveAnnouncement = () => {
/* retrieve announcements */
firebase.firestore().collection("announcement").get().then((querySnapshot) => {
querySnapshot.forEach((doc) => {
this.state.content.push(doc.data().CONTENT);
}).catch(err => console.log(err));
});
};
render() {
return (
<View>
<Button onPress={this.samplePrint} title="Print"></Button>
</View>
)
}
componentWillMount(){
/* similar to mounted in vue */
this.retrieveAnnouncement();
}
retrieveAnnouncement = () => {
const announcements = [];
/* retrieve announcements */
firebase.firestore().collection("announcement").get().then((querySnapshot) => {
querySnapshot.forEach((doc) => {
announcements.push(doc.data().CONTENT);
}).catch(err => console.log(err));
});
this.setState({content: announcements});
};
render() {
return (
<View>
<Button onPress={this.samplePrint} title="Print"></Button>
<Text>{this.state.content}</Text>
</View>
)
}
您可以更改您的 firebase 代码:
retrieveAnnouncement = () => {
const announcements = [];
/* retrieve announcements */
firebase
.firestore()
.collection('announcement')
.get()
.then(querySnapshot => {
querySnapshot.forEach(doc => {
announcements.push(doc.data().CONTENT);
});
this.setState({ content: announcements });// this will set announcements and triger render.
})
.catch(err => console.log(err));
}
在此之后,您可以在渲染方法中使用内容数组。
render (){
console.log (this.state.content);
const listItems = this.state.content.map((data) => {
return (
<View><Text>{data}</Text></View>
)
})
return (
<View>
<Button onPress={this.samplePrint} title="Print"></Button>
{listItems}
</View>
)
}
希望这对解决您的问题有所帮助。
state = {
content: []
}
componentWillMount(){
/* similar to mounted in vue */
this.retrieveAnnouncement()
}
retrieveAnnouncement = () => {
/* retrieve announcements */
let sampleData = firebase.firestore().collection("announcement").get()
.then((querySnapshot) => {
if (querySnapshot.exists) {
return { success: true, data: doc.data() }
} else {
return { success: false, message: "Not Found" }
}
}).catch(error => {
return { success: false, message: error}
});
return sampleData
};
render() {
return (
<View>
<Button onPress={this.samplePrint} title="Print"></Button>
</View>
)
}
确保在每个条件中添加 return
希望这有效
如何在 React Native 中呈现来自我的 firestore 数据库的数据?
下面是我的代码,它只将数据推送到数组中,但我不知道如何在 Card 中使用它。我尝试使用 Flatlist 但失败了。
截至目前,每当我尝试在控制台中打印数据时,它看起来像这样:
Array [ "VD GOT JEALOUS", "Sample Content", ]
我希望该数组在 Card 或 Text 组件中呈现
state = {
content: []
}
componentWillMount(){
/* similar to mounted in vue */
this.retrieveAnnouncement()
}
retrieveAnnouncement = () => {
/* retrieve announcements */
firebase.firestore().collection("announcement").get().then((querySnapshot) => {
querySnapshot.forEach((doc) => {
this.state.content.push(doc.data().CONTENT);
}).catch(err => console.log(err));
});
};
render() {
return (
<View>
<Button onPress={this.samplePrint} title="Print"></Button>
</View>
)
}
componentWillMount(){
/* similar to mounted in vue */
this.retrieveAnnouncement();
}
retrieveAnnouncement = () => {
const announcements = [];
/* retrieve announcements */
firebase.firestore().collection("announcement").get().then((querySnapshot) => {
querySnapshot.forEach((doc) => {
announcements.push(doc.data().CONTENT);
}).catch(err => console.log(err));
});
this.setState({content: announcements});
};
render() {
return (
<View>
<Button onPress={this.samplePrint} title="Print"></Button>
<Text>{this.state.content}</Text>
</View>
)
}
您可以更改您的 firebase 代码:
retrieveAnnouncement = () => {
const announcements = [];
/* retrieve announcements */
firebase
.firestore()
.collection('announcement')
.get()
.then(querySnapshot => {
querySnapshot.forEach(doc => {
announcements.push(doc.data().CONTENT);
});
this.setState({ content: announcements });// this will set announcements and triger render.
})
.catch(err => console.log(err));
}
在此之后,您可以在渲染方法中使用内容数组。
render (){
console.log (this.state.content);
const listItems = this.state.content.map((data) => {
return (
<View><Text>{data}</Text></View>
)
})
return (
<View>
<Button onPress={this.samplePrint} title="Print"></Button>
{listItems}
</View>
)
}
希望这对解决您的问题有所帮助。
state = {
content: []
}
componentWillMount(){
/* similar to mounted in vue */
this.retrieveAnnouncement()
}
retrieveAnnouncement = () => {
/* retrieve announcements */
let sampleData = firebase.firestore().collection("announcement").get()
.then((querySnapshot) => {
if (querySnapshot.exists) {
return { success: true, data: doc.data() }
} else {
return { success: false, message: "Not Found" }
}
}).catch(error => {
return { success: false, message: error}
});
return sampleData
};
render() {
return (
<View>
<Button onPress={this.samplePrint} title="Print"></Button>
</View>
)
}
确保在每个条件中添加 return 希望这有效