【发布时间】:2015-08-29 07:55:57
【问题描述】:
我在将 Firebase 与 React-Native 集成时遇到了问题。下面的代码没有像我预期的那样生成列表视图。我的假设是 messages.val() 没有返回正确的格式。当我尝试控制台记录“消息”变量时,它返回如下
Object {text: "hello world", user_id: 1}
代码:
class Test extends Component {
constructor(props) {
super(props);
this.state = {
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2
})
};
}
componentWillMount() {
this.dataRef = new Firebase("https://dummy.firebaseio.com/");
this.dataRef.on('child_added', function(snapshot){
var messages = snapshot.val();
this.setState({
dataSource: this.state.dataSource.cloneWithRows(messages)
});
}.bind(this));
}
renderRow(rowData, sectionID, rowID) {
console.log(this.state.dataSource);
return (
<TouchableHighlight
underlayColor='#dddddd'>
<View>
<Text>{rowData.user_id}</Text>
<Text>{rowData.text}</Text>
</View>
</TouchableHighlight>
)
}
render() {
return (
<View>
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderRow.bind(this)}
automaticallyAdjustContentInsets={false} />
</View>
);
}
}
【问题讨论】:
标签: javascript ios listview firebase react-native