Fix state not updating properly in the list view
This commit is contained in:
parent
a1c9604db9
commit
247908b1ba
@ -9,34 +9,51 @@ import ListPoster from './poster'
|
|||||||
|
|
||||||
import Loader from '../loader/loader'
|
import Loader from '../loader/loader'
|
||||||
|
|
||||||
|
const DEFAULT_LIST_SIZE = 30;
|
||||||
|
const DEFAULT_ADD_EXTRA_ITEMS = 30;
|
||||||
|
|
||||||
export default class ListPosters extends React.Component {
|
export default class ListPosters extends React.Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
this.state = {
|
this.state = {
|
||||||
items: 30,
|
items: 0,
|
||||||
hasMore: true,
|
hasMore: false,
|
||||||
};
|
};
|
||||||
this.loadMore = this.loadMore.bind(this);
|
this.loadMore = this.loadMore.bind(this);
|
||||||
}
|
}
|
||||||
loadMore() {
|
loadMore() {
|
||||||
if (!this.state.hasMore || this.props.data.length === 0) {
|
// Nothing to do if the app is loading
|
||||||
return
|
if (this.props.loading) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const addItems = 30;
|
if (!this.props.data.length) {
|
||||||
if (this.state.items + addItems >= this.props.data.length) {
|
return;
|
||||||
this.setState({
|
|
||||||
items: this.props.data.length,
|
|
||||||
hasMore: false,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
this.setState({
|
|
||||||
items: this.state.items + addItems,
|
|
||||||
hasMore: true,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.setState(this.getNextState(this.props));
|
||||||
|
}
|
||||||
|
getNextState(props) {
|
||||||
|
let totalListSize = props.data.length;
|
||||||
|
let currentListSize = this.state.items;
|
||||||
|
let nextListSize = currentListSize + DEFAULT_ADD_EXTRA_ITEMS;
|
||||||
|
let hasMore = true;
|
||||||
|
|
||||||
|
if (nextListSize >= totalListSize) {
|
||||||
|
nextListSize = totalListSize;
|
||||||
|
hasMore = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
items: nextListSize,
|
||||||
|
hasMore: hasMore,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
componentWillReceiveProps(nextProps) {
|
||||||
|
if (this.props.data.length !== nextProps.data.length) {
|
||||||
|
this.setState(this.getNextState(nextProps));
|
||||||
|
}
|
||||||
|
}
|
||||||
render() {
|
render() {
|
||||||
let elmts = this.props.data.slice();
|
let elmts = this.props.data.slice();
|
||||||
const listSize = elmts.length;
|
const listSize = elmts.length;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user