我有這個功能:
const handleSearch = (event) => {
const currentValue = event?.target?.value;
const filteredData = searchTable(currentValue, originalData);
setDataToTable(filteredData);
};
我試圖用來useMemo()記住 的值,filteredData以便不執行該功能,searchTable因為它很慢。我在handleSearch函式內部試過這個:
const filteredData = useMemo(() => searchTable(currentValue, originalData), [currentValue]);
但我得到了Hooks can only be called inside of the body of a function component正確的資訊。
我應該如何使用useMemo我的情況?
uj5u.com熱心網友回復:
您必須將搜索查詢存盤到 a 中state variable并useMemo與此變數一起使用以計算過濾后的資料。
function App() {
const [query, setQuery] = useState();
const filteredData = useMemo(() => searchTable(query, originalData), [query]);
function handleSearch(event) {
setQuery(event?.target?.value);
}
return (...)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/395210.html
