【发布时间】:2016-11-19 17:41:22
【问题描述】:
我正在尝试使用 React Native ListView 组件,但它没有渲染。 ListView 采用 dataBlob 这是一个数组,这就是我要传递给它的内容。
在我的主要组件中,我使用componentWillMount 从 Firebase 获取数据。这给了我一个包含消息和发件人的对象数组。
示例:[{text: 'Hello', sender: 'p1'}, {text: 'Hi', sender: 'p2}]
主要组件:
constructor(props) {
super(props);
this.state = {
messages: []
}
}
componentWillMount() {
app.database().ref('messages/').on('child_added', function(snapshot) {
let data = snapshot.val();
for(let i in data) {
this.state.messages.push(data[i]);
}
this.setState({messages: this.state.messages});
}.bind(this));
}
render() {
return (
<View>
<MessagesList messages={this.state.messages}/>
</View>
)
}
消息列表组件:
constructor(props) {
super(props);
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2 });
this.state = {
dataSource: ds.cloneWithRows(props.messages)
}
}
renderRows = (data) => {
return (
<View>
<Text>{data.text}</Text>
</View>
)
}
render() {
return(
<ListView
dataSource={this.state.dataSource}
renderRow={(data) => this.renderRows(data)}
enableEmptySections={true}
/>
)
}
当我将对象数组硬编码到 dataSource 中时,它可以工作,但不适用于主组件的数组传递。
【问题讨论】:
标签: reactjs firebase react-native firebase-realtime-database