[英]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
.但过滤后的数据应该只是从
query
和properties
的状态(或道具)派生的变量。
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
仅在query
或properties
更改时重新计算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 而不是检查
properties
和query
是否存在。 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.