另一种无限下拉
import {components, config} from "common-utils";
import style from './css/refuse.less';
import {Component} from 'refast';
import Empty from './empty/Empty';
import loadingImg from './imgage/loading.png'
export default class Refuse extends Component {
constructor(props) {
super(props);
let { data = [], current = 1, total = 0, sensitivity = 10 , loading = true} = this.props;
this.state = {
loading,
sensitivity,
data,
current,
total,
}
}
componentWillReceiveProps(nextProps, nextContext) {
let { data, current, total, loading } = nextProps;
if( data !== this.state.data ){
this.setState({
data,
loading:false
}, this.needDom )
}
if( current !== this.props.current || total !== this.props.total ) this.setState({ current, total })
if( loading !== this.props.loading ) this.setState({ loading: nextProps.loading })
}
needDom = () =>{
let {vListContainer , phantomContent } = this.refs,
out_height = vListContainer?.clientHeight ?? 0,
in_height = phantomContent?.clientHeight ?? 0;
if( out_height > in_height && !this.state.loading && this.compare() ) this.getData()
};
compare = () => {
let { data = [], total } = this.state;
return (data.length || data.size || 0) < total
};
getData = () => { this.setState({ loading: true },()=>this.props?.addData( this.state?.current + 1 )) }
onScrollList = (e) => {
let { sensitivity, loading } = this.state;
let scroll_total_height = this.refs?.phantomContent?.scrollHeight??0;
let scroll_office = e?.target?.scrollTop??0;
let out_height = this.refs?.vListContainer?.clientHeight??0;
if(scroll_office + out_height + sensitivity > scroll_total_height && this.compare() && !loading) this.getData()
};
render() {
return (
<div className={style.refuse}>
<div className={ this.state.loading ? 'vListContainer_loading' : 'vListContainer_loading loading_none' } >
<img src={ loadingImg } />
</div>
<div className={'vListContainer '}
onScroll={(e)=> { e.preventDefault(); this.onScrollList(e) } }
ref = {'vListContainer'} >
{ this.props.children ? <div className={'phantomContent'} ref = {'phantomContent'}> { this.props.children} </div> : <Empty /> }
</div>
</div>
);
}
}