firebase - 从 firebase-React-Native 渲染的问题
问题描述
我有这个问题:我必须渲染一个 FlatList 映射 Firestore 文档,我这样做:
componentDidMount();
{
const cat = [];
var db = firebase.firestore();
const ristoDoc = db.collection("ristoranti").where("telefono", "==", "3296812820");
ristoDoc.get().then((snapshot) => {
snapshot.forEach((doc) => {
this.setState({
id: doc.id,
});
});
});
db.collection("ristoranti")
.doc(this.state.id)
.collection("categorie")
.onSnapshot((querysnapshot) => {
querysnapshot.forEach((doc) => {
const data = doc.data();
this.setState({
nome: data,
});
cat.push(data);
});
this.setState({
categorie: cat,
});
});
}
还有我的平面列表:
<
FlatList horizontal = {
true
}
pagingEnabled = {
true
}
style = {
{
width: '100%',
height: '22%'
}
}
contentContainerStyle = {
{
height: '90%',
width: 100
}
}
data = {
this.state.categorie
}
renderItem = {
(item) =>
{
<View>
> <Text key={item.id}> {item.nome} </Text>
<View>
<TouchableOpacity onPress = {
() => this.setState(
{
visibileProdottoAdd: true
})
}>
<Text> + </Text> </TouchableOpacity> </View> </View>
但是我总是遇到相同的图像错误[ ]我尝试了一整天,没有任何改变我在 item.name 上做了一个小错误,原始代码中有大括号
更新
<FlatList
horizontal={true}
pagingEnabled={true}
style={{ width: "100%", height: "22%" }}
contentContainerStyle={{ height: "90%", width: 100 }}
data={this.state.categorie}
key={({ item }) => {
item.id;
}}
renderItem={({ item }) => {
<View
style={{
flexDirection: "row",
justifyContent: "flex-start",
alignItems: "center",
width: 200,
}}
>
<Text
key={item.id}
style={{
fontSize: 20,
color: "#C6C6C6",
marginBottom: 5,
width: "35%",
}}
>
{item.nome}
</Text>
<View
style={{
flexDirection: "row",
justifyContent: "flex-end",
alignItems: "center",
width: "60%",
}}
>
<TouchableOpacity
onPress={() =>
this.setState({ visibileProdottoAdd: true })
}
>
<Text style={{ fontSize: 20, color: "#66a3ff" }}>
+
</Text>
</TouchableOpacity>
</View>
</View>;
}}
></FlatList>
我在这个版本中编辑我的代码,没有任何变化,在 componentdidmount 函数中我尝试测试从 Firestore 获得的内容
componentDidMount() {
const cat = [];
var db = firebase.firestore();
const ristoDoc = db
.collection("ristoranti")
.where("telefono", "==", "3296812820");
ristoDoc.get().then((snapshot) => {
snapshot.forEach((doc) => {
this.setState({ id: doc.id });
});
});
db.collection("ristoranti")
.doc("qKEWUfZPsy2pu6IoFUio")
.collection("categorie")
.onSnapshot((querysnapshot) => {
querysnapshot.forEach((doc) => {
const data = doc.data();
this.setState({ nome: data });
cat.push(data);
cat.forEach(({i})=>{console.log(i.nome)})
});
this.setState({ categorie: cat });
});
奇怪的是,这是 Expo Cli 上的错误,但在我的 Visual Studio 控制台日志中,我有我的姓名列表。
解决方案
你犯了一个非常简单的错误。我认为问题出在渲染项中。
<FlatList
data={arr}
key={({ item }) => item}
onEndReached={({ index }) => handleLoadMore(index)}
renderItem={({item} ) => <RenderItem item={item} />}
/>
你正在传递一个项目。你必须通过对象 {item}
推荐阅读
- c# - 尝试使用匿名类型生成自定义 JSON
- robotframework - 如何查找是否已定义变量?
- reactjs - 带有 ActionCable (WS) 和 React 前端的 Rails API
- java - 模拟单元测试和工厂
- java - Websphere 9,如何检查传入的 ltpatoken2
- php - 如何通过url动态更改输入值
- python - 为什么运行GPT2小模型时找不到encoder.json
- python - 使用python在cmd中找不到'__main__'模块
- c - 为什么 printf 在循环中只打印一次,只针对第一个输入?
- python - 为什么我无法使用 Python3 发送带有 excel 附件的电子邮件