React功能组件中的lodash抖动不起作用

时间:2019-04-10 15:41:26

标签: reactjs debounce

我有一个围绕React Table组件构建的功能组件,该组件使用Apollo GraphQL client进行服务器端分页和搜索。我正在尝试为搜索实现反跳操作,以便一旦用户停止使用该值键入内容,就只对服务器执行一个查询。我已经尝试过lodash debounceawesome debounce promise解决方案,但是仍然针对服务器在搜索字段中键入的每个字符都执行了查询。

这是我的组件(删除了不相关的信息):

import React, {useEffect, useState} from 'react';
import ReactTable from "react-table";
import _ from 'lodash';
import classnames from 'classnames';
import "react-table/react-table.css";
import PaginationComponent from "./PaginationComponent";
import LoadingComponent from "./LoadingComponent";
import {Button, Icon} from "../../elements";
import PropTypes from 'prop-types';
import Card from "../card/Card";
import './data-table.css';

import debounce from 'lodash/debounce';

function DataTable(props) {
    const [searchText, setSearchText] = useState('');
     const [showSearchBar, setShowSearchBar] = useState(false);

    const handleFilterChange = (e) => {
        let searchText = e.target.value;
        setSearchText(searchText);
        if (searchText) {
            debounceLoadData({
                columns: searchableColumns,
                value: searchText
            });
        }
    };

    const loadData = (filter) => {
        // grab one extra record to see if we need a 'next' button
        const limit = pageSize + 1;
        const offset = pageSize * page;

        if (props.loadData) {
            props.loadData({
                variables: {
                    hideLoader: true,
                    opts: {
                        offset,
                        limit,
                        orderBy,
                        filter,
                        includeCnt: props.totalCnt > 0
                    }
                },
                updateQuery: (prev, {fetchMoreResult}) => {
                    if (!fetchMoreResult) return prev;
                    return Object.assign({}, prev, {
                        [props.propName]: [...fetchMoreResult[props.propName]]
                    });
                }
            }).catch(function (error) {
                console.error(error);
            })
        }
    };

    const debounceLoadData = debounce((filter) => {
        loadData(filter);
    }, 1000);

    return (
        <div>
            <Card style={{
                border: props.noCardBorder ? 'none' : ''
            }}>
                {showSearchBar ? (
                        <span className="card-header-icon"><Icon className='magnify'/></span>
                        <input
                            autoFocus={true}
                            type="text"
                            className="form-control"
                            onChange={handleFilterChange}
                            value={searchText}
                        />
                        <a href="javascript:void(0)"><Icon className='close' clickable
                                                           onClick={() => {
                                                               setShowSearchBar(false);
                                                               setSearchText('');
                                                           }}/></a>
                ) : (
                        <div>
                           {visibleData.length > 0 && (
                                <li className="icon-action"><a 
href="javascript:void(0)"><Icon className='magnify' onClick= {() => {
    setShowSearchBar(true);
    setSearchText('');
}}/></a>
                                </li>
                            )}
                        </div>
                    )
                )}
                <Card.Body className='flush'>
                    <ReactTable
                        columns={columns}
                        data={visibleData}
                    />
                </Card.Body>
            </Card>
        </div>
    );
}

export default DataTable

...这就是结果:link

3 个答案:

答案 0 :(得分:10)

MyName将成为每个渲染的新功能。您可以使用useCallback钩子来确保在渲染之间保留相同的功能,并且该功能可以按预期工作。

debounceLoadData

useCallback(debounce(loadData, 1000), []);
const { useState, useCallback } = React;
const { debounce } = _;

function App() {
  const [filter, setFilter] = useState("");
  const debounceLoadData = useCallback(debounce(console.log, 1000), []);

  function handleFilterChange(event) {
    const { value } = event.target;

    setFilter(value);
    debounceLoadData(value);
  }

  return <input value={filter} onChange={handleFilterChange} />;
}

ReactDOM.render(<App />, document.getElementById("root"));

答案 1 :(得分:3)

要添加到Tholle的答案中:如果您想充分利用钩子并使代码更紧凑,可以使用useEffect钩子来监视过滤器中的更改并运行{{1 }}在发生这种情况时起作用:

debouncedLoadData

答案 2 :(得分:0)

您必须记住渲染之间的去抖动功能。

但是,您不应该不要使用useCallback来记住其他答案中建议的去抖动(或限制)功能。 useCallback专为内联函数设计!

使用useMemo来记住渲染之间的去抖动功能:

useMemo(() => debounce(loadData, 1000), []);