【发布时间】:2018-11-10 11:56:30
【问题描述】:
我正在创建一个购物清单应用程序,其中前端是 react-native,后端是 firebase。在购物清单中要添加项目。我已经在 firebase 中存储了一些项目,并从 firestore 中检索了这些项目,但它显示了一个错误:
对象作为 React 子对象无效(找到:带有键的对象($$typof,key,ref,props,_owner,_store)。如果您要渲染子对象的集合,请改用数组。
我正在使用 react-native 0.55.4 版本。请帮帮我。
这是 index.js 文件
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
View,
ListView,TouchableHighlight,AppRegistry
} from 'react-native';
import * as firebase from 'firebase';
import ToolBar from './app/components/ToolBar/ToolBar';
const styles = require('./app/style');
const firebaseConfig = {
apiKey: 'AIzaSyClhqB9TEEffc4szIHUEVt0OqXvKQOEwxQ',
authDomain: 'grocerryapp-fbe06.firebaseapp.com',
databaseURL: 'https://grocerryapp-fbe06.firebaseio.com',
projectId: 'grocerryapp-fbe06',
storageBucket: 'grocerryapp-fbe06.appspot.com',
messagingSenderId: '903884995524'
}
const firebaseApp = firebase.initializeApp(firebaseConfig);
export default class App extends Component {
constructor(){
super();
let ds= new ListView.DataSource({rowHasChanged:(r1,r2) => r1 !== r2});
this.state={
itemDataSource:ds
}
this.itemsRef = this.getRef().child('items');
this.renderRow = this.renderRow.bind(this);
this.pressRow = this.pressRow.bind(this);
}
getRef(){
return firebaseApp.database().ref();
}
componentWillMount() {
this.getItems(this.itemsRef);
}
componentDidMount() {
this.getItems(this.itemsRef);
}
getItems(itemsRef) {
//let items = [{title: 'Item One'},{title: 'Item Two'}];
itemsRef.on('value',(snap) => {
let items = [];
snap.forEach((child) => {
items.push({
title: child.val().title,
_key: child.key
});
});
this.setState({
itemDataSource: this.state.itemDataSource.cloneWithRows(items)
});
});
}
pressRow(item){
console.log(item);
}
renderRow(item){
return(
<TouchableHighlight onPress={() => {
this.pressRow(item);
}}>
<View style={styles.li}>
<Text style={styles.liText}>{item.title}</Text>
</View>
</TouchableHighlight>
);
}
render() {
return (
<View style={styles.container}>
<ToolBar title="Item Lister" />
<ListView
dataSource={this.state.itemDataSource}
renderRow={this.renderRow}
/>
</View>
);
}
}
AppRegistry.registerComponent('itemlister', () => 'itemlister');
【问题讨论】:
-
不推荐使用
FlatList,ListView。 -
谢谢....但同样的错误显示
-
除了远离它之外,我对反应一无所知,......虽然看代码 - 我的问题是,为什么要填充一个数组然后将它转储到 UI 时你可以直接将项目转储到 UI 吗? ...只是我的小 2 美分。
-
顺便说一句,我强烈建议使用react-native-firebase。它具有与网页版相同的 API(尽可能接近),同时支持 4 倍于移动应用的 firebase 功能
标签: javascript firebase react-native firebase-realtime-database