gpt4 book ai didi

react-native - React Native ListView - rowHasChanged 不会触发

转载 作者:行者123 更新时间:2023-12-03 14:26:46 25 4
gpt4 key购买 nike

我正在尝试在 React Native 中实现无限滚动。以下是组件的来源:

var React = require('react-native');
var server = require('../server');
var Post = require('./Post');
var SwipeRefreshLayoutAndroid = require('./SwipeRefreshLayout');
var backEvent = null;
var lastPostId = "";
var isLoadingMore = false;
var isLoadingTop = false;
var onEndReachedActive = false;

var {
StyleSheet,
ListView,
View,
Text,
Image,
ProgressBarAndroid,
BackAndroid
} = React;

class Stream extends React.Component {

constructor(props) {
super(props);
this.ds = new ListView.DataSource({
rowHasChanged: (row1, row2) => {
console.log("rowHasChenged FIRED!!");
return false;
}
});

this.state = {
dataSource: this.ds.cloneWithRows(['loader']),
hasStream: false,
posts: []
};

}

componentDidMount() {

BackAndroid.addEventListener('hardwareBackPress', () => {
this.props.navigator.jumpBack();
return true;
}.bind(this));

server.getStream('', '', 15).then((res) => {

lastPostId = res[res.length-1].m._id;

this.setState({
posts: res,
hasStream: true,
dataSource: this.ds.cloneWithRows(res)
}, () => onEndReachedActive = true);
})
}

onRefresh() {
var posts = this.state.posts;
var firstPost = posts[0].m._id;

console.log(this.state.dataSource._rowHasChanged);

isLoadingTop = true;

server.getStream('', firstPost, 4000)
.then(res => {
console.log(posts.length);
posts = res.concat(posts);
console.log(posts.length);
this.setState({
dataSource: this.ds.cloneWithRows(posts),
posts
}, () => {
this.swipeRefreshLayout && this.swipeRefreshLayout.finishRefresh();
isLoadingTop = false;
});
}).catch((err) => {
isLoadingTop = false;
})

}

onEndReached(event) {

if(!onEndReachedActive) return;

if(this.state.loadingMore || this.state.isLoadingTop)return;
isLoadingMore = true;
var posts = this.state.posts;
server.getStream(posts[posts.length-1].m._id, '', 15)
.then(res => {
console.log('received posts');
posts = posts.concat(res);
lastPostId = posts[posts.length-1].m._id;
this.setState({
dataSource: this.ds.cloneWithRows(posts),
posts
}, ()=>isLoadingMore = false);
})
}

renderHeader() {
return (
<View style={styles.header}>
<Text style={styles.headerText}>Header</Text>
</View>
)
}

renderRow(post) {

if(post === 'loader') {
return (
<ProgressBarAndroid
styleAttr="Large"
style={styles.spinnerBottom}/>
)
}

let hasLoader = post.m._id === lastPostId;

let loader = hasLoader ?
<ProgressBarAndroid
styleAttr="Large"
style={styles.spinnerBottom}/> : null;

return (
<View>
<Post
post={post}/>
{loader}
</View>
)
}

render() {


return (
<ListView
style={styles.mainContainer}
dataSource={this.state.dataSource}
renderRow={this.renderRow.bind(this)}
onEndReached={this.onEndReached.bind(this)}
onEndReachedThreshold={1}
pageSize={15} />
);
}
}

问题是,每当我添加(或添加)新数据时, rowHasChanged DataSource的方法不火。它只是重新渲染每一行,即使没有任何变化(新数据除外)。
知道为什么绕过该方法吗?

最佳答案

编辑:将函数传递给 setState 以避免竞争条件

我只是想通了。如果您遇到同样的问题,请使用新的 dataSource 检查您更改状态的点。 .我的是这样的:

this.setState({
dataSource: this.ds.cloneWithRows(posts)
});

相反,您应该始终使用 dataSource从以前的状态,像这样:
this.setState(state => ({
dataSource: state.dataSource.cloneWithRows(posts)
}))

干杯!

关于react-native - React Native ListView - rowHasChanged 不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33870403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com