如题目,我用了ListView这个组件,但是一直出问题:
var MessageList = React.createClass({
getInitialState: function() {
return {
dataSource: new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}),
isLoadingTail: false
}
},
componentDidMount: function () {
this._messages = [];
this.setState({
dataSource: this.getDataSource(this.props.messageList)
});
},
getDataSource: function (messages):ListView.DataSource {
this._messages = this._messages.concat(messages);
return this.state.dataSource.cloneWithRows(this._messages);
},
_onEndReached: function () {
console.log('onEndReached', this.state.isLoadingTail);
if (this.state.isLoadingTail) {
// We're already fetching
return;
}
this.setState({
isLoadingTail: true
});
this.setState({
isLoadingTail: false,
dataSource: this.getDataSource(this.props.messageList)
});
},
_renderRow: function(rowData, sectionID, rowID){
return (
<Text>{rowData}</Text>
)
},
render: function() {
return (
<ListView
dataSource={this.state.dataSource}
renderRow={this._renderRow}
onEndReached={this._onEndReached}
style={MessageListStyle.container}
/>
);
}
});
问题在 this.props.messageList
在 render 输入是不为空的数组,但是在componentDidMount
函数中却变成了一个空的数组,请问这是怎么回事啊?应该怎么解决?
期待你的帮忙。
componentDidMount 改成 componentWillMount 试一下