美文网首页
react防抖节流

react防抖节流

作者: 我是七月 | 来源:发表于2022-11-07 21:28 被阅读0次

节流----throttle

方法一
import throttle from 'lodash/throttle';
export default class Search extends Component {
    constructor(props) {
        super(props)
        this.handleClick = throttle(this.handleClick , 1000);
    }
    handleClick = (e) => {
        ...
    }
}
    render() {
        return (
        <div onClick={this.handleClick}><div>
        )
    }
}

方法二
import Throttle from 'lodash-decorators/throttle';
export default class Search extends Component {
    constructor(props) {
        super(props)
        this.handleClick = this.handleClick .bind(this);
    }
    @Throttle(300)
    handleClick = (e) => {
        ...
    }
}
    render() {
        return (
        <div onClick={this.handleClick}><div>
        )
    }
}


防抖----debounce

方法一
import debouncefrom 'lodash/debounce';
export default class Search extends Component {
    constructor(props) {
        super(props)
        this.handleClick = debounce(this.handleClick , 1000);
    }
    handleClick = (e) => {
        ...
    }
}
    render() {
        return (
        <div onClick={this.handleClick}><div>
        )
    }
}


方法二
import Throttle from 'lodash-decorators/debounce';
export default class Search extends Component {
    constructor(props) {
        super(props)
        this.handleClick = this.handleClick .bind(this);
    }
    @debounce(300)
    handleClick = (e) => {
        ...
    }
}
    render() {
        return (
        <div onClick={this.handleClick}><div>
        )
    }
}

相关文章

网友评论

      本文标题:react防抖节流

      本文链接:https://www.haomeiwen.com/subject/zheitdtx.html