简体   繁体   English

加载器同时使用关键字过滤数据

[英]Loader while filtering data with keywords

I want to show a loader when I search properties with the filter method.当我使用过滤器方法搜索属性时,我想显示一个加载器。 When somebody searches it should show a loading spinner when the filter is complete it should show the data.. I'm not filtering data with API calls.当有人搜索时,它应该在过滤器完成时显示加载微调器,它应该显示数据。我没有使用 API 调用过滤数据。 I have used react hooks for a loading state and it is true by default.我已经使用反应钩子来加载 state 并且默认情况下是真的。 If anyone can help me how can I use the setState hook inside a function to change the state, if I use setState in the function it will re-render again and again which is not allowed.if anyone of you can help please help如果有人可以帮助我,我如何使用 function 中的 setState 挂钩来更改 state,如果我在 function 中使用 setState,请再次提供帮助

 export const applyFilters = (properties, query) => { return ( properties && properties.filter((property, i) => { let matches = true; if (query) { const keys = ["title"]; let containsQuery = false; keys.forEach(key => { if (property[key].toLowerCase().includes(query.toLowerCase())) { containsQuery = true; } }); if (;containsQuery) { matches = false; } } return matches; }) ); };
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

Your filtering is a synchronous operation so it should be almost instantaneous.您的过滤是一个同步操作,所以它应该几乎是瞬时的。 I would not show a spinner at all.我根本不会展示微调器。

Generally I recommend that the filter itself should be a state.一般来说,我建议过滤器本身应该是 state。 That would be your query variable.那将是您的query变量。 But the filtered data should just be a variable derived from the states (or props) of query and properties .但过滤后的数据应该只是从queryproperties的状态(或道具)派生的变量。

export const FilterProperties = () => {
  const [query, setQuery] = useState("");
  // some state that gets set somehow, or maybe properties is a prop
  const [properties, setProperties] = useState([]);

  const filteredProperties = applyFilters(properties, query);

  return (
    <div>
      <input value={query} onChange={(e) => setQuery(e.target.value)} />
      <ul>
        {filteredProperties.map((property) => (
          <li key={property.id}>{property.title}</li>
        ))}
      </ul>
    </div>
  );
};

If you are concerned about performance then you can use useMemo to re-compute the filteredProperties variable only when query or properties changes.如果您担心性能,那么您可以使用useMemo仅在queryproperties更改时重新计算filteredProperties的Properties 变量。

const filteredProperties = useMemo(
  () => applyFilters(properties, query), 
  [properties, query]
);

The filter function itself can be cleaned up quite a bit.过滤器 function 本身可以清理很多。 We can use default arguments instead of checking that properties and query exist.我们可以使用默认的 arguments 而不是检查propertiesquery是否存在。 We can use an empty array for properties to return no matches and an empty string for query which matches everything.我们可以为properties使用一个空数组以不返回匹配项,并为与所有内容匹配的query使用一个空字符串。

If you always just want to search the title then this keys array is pointless:如果您总是只想搜索title ,那么此keys数组毫无意义:

const applyFilters = (properties = [], query = "") =>
    properties.filter((property) =>
        property.title.toLowerCase().includes(query.toLowerCase())
    );

But we can make keys an argument to allow more customization.但是我们可以将keys作为参数来允许更多的定制。 We make use of the Array.prototype.some() method to see if any of the keys are a match.我们使用Array.prototype.some()方法来查看是否有任何键匹配。

const applyFilters = (properties = [], query = "", keys = ["title"]) =>
    properties.filter((property) =>
        // true if any key contains the query
        keys.some((key) =>
            property[key].toLowerCase().includes(query.toLowerCase())
        )
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 数据加载时显示加载器图标 - Display loader icon while data is loading 使用静态关键字的合适的 webpack 加载器是什么? - What is the appropriate webpack loader to use static keywords? 在react-redux组件中获取初始数据时显示加载器 - show loader while fetching initial data in react-redux component 在 MUIDatatable 中分页或过滤期间未显示加载程序 - Loader is not showing during paging or filtering in MUIDatatable 如何在排序和过滤数据时为反应组件设置加载 state? - How to set a loading state for react component while sorting and filtering data? 过滤时找不到数据时如何显示错误消息 - How to show an error message when no data is found while filtering 在我们等待响应时如何显示加载器,如果没有数据来显示消息“没有可用数据”? - How to display loader while we wait for the response and if no data comes display a message “No data available”? 如何在加载大页面-data.json 时在 Gatsby 站点上显示加载器? - How to show loader on a Gatsby site while large page-data.json is loading? 从“react-loader-spinner”导入 Loader 时出错? - Getting an error while importing Loader from 'react-loader-spinner'? React-在子节点加载时显示加载器 - React - show loader while child nodes are loading
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM