[英]react-hooks/exhaustive-deps causing dependency warning, fix hangs code
Im working on a project and have incorporated Hooks for the first time.我正在做一个项目,并且第一次合并了 Hooks。 When using the useEffects and useState hooks, Im encountering a wierd warning from eslint.
使用 useEffects 和 useState 挂钩时,我遇到了来自 eslint 的奇怪警告。
My Code:我的代码:
import React, { useState, useEffect } from 'react';
import { Card } from 'antd';
import Search from 'antd/lib/input/Search';
import { IPatient } from 'types/IPatient';
const SearchBox = () => {
const [searchTerm, setSearchTerm] = useState('');
const [searchResults, setSearchResults] = useState<IPatient[]>([]);
const handleChange = (event: any) => {
setSearchTerm(event.target.value);
};
const cards: IPatient[] = [
{
id: 1,
name: 'Erling',
description: ['tall', 'male', 'sick'],
age: 98,
isHuman: true,
},
// other data...
];
useEffect(() => {
const results: IPatient[] = cards.filter((card) =>
card.name.toLowerCase().includes(searchTerm),
);
setSearchResults(results);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [searchTerm]);
return (
<>
<div className="searchbox">
<Search onChange={handleChange} />
</div>
<div>
{searchResults.map((data) => (
<Card key={data.id} hoverable>
<h1>{data.name}</h1>
<p>Patient ID: {data.id} </p>
<p>Age: {data.age} years old.</p>
<p>
Description:{' '}
{data.description[0] +
' ' +
data.description[1] +
' ' +
data.description[2]}
</p>
</Card>
))}
</div>
</>
);
};
export default SearchBox;
Now, the issue is that eslint is calling an error on my dependency array, and if I put both variables (cards and searchTerms) inside the array, it results in the code hanging and the webapp crashing.现在,问题是 eslint 在我的依赖数组上调用了一个错误,如果我将两个变量(cards 和 searchTerms)都放在数组中,它会导致代码挂起和 webapp 崩溃。 The eslint-line is currently in place to suppress the warning, but this is less than ideal.
eslint-line 目前已到位以抑制警告,但这并不理想。
So I guess my question is how to circumvent this.所以我想我的问题是如何规避这个问题。 I am sure this a Beginners mistake, as it is my first time with Hooks.
我确信这是初学者的错误,因为这是我第一次使用 Hooks。 Any help would be appreciated!
任何帮助,将不胜感激!
The problem when adding cards to the dependency array is that you are creating a new reference of cards array on each rerender and hence the useEffect runs again, causing an infinite loop将卡片添加到依赖数组时的问题是,您在每次重新渲染时都创建了卡片数组的新引用,因此 useEffect 再次运行,导致无限循环
Since Card array seems to be const you can take it out of functional component and then add it to dependency array由于 Card 数组似乎是 const 您可以将其从功能组件中取出,然后将其添加到依赖数组中
const cards: IPatient[] = [
{
id: 1,
name: 'Erling',
description: ['tall', 'male', 'sick'],
age: 98,
isHuman: true,
},
// other data...
];
const SearchBox = () => {
const [searchTerm, setSearchTerm] = useState('');
const [searchResults, setSearchResults] = useState<IPatient[]>([]);
const handleChange = (event: any) => {
setSearchTerm(event.target.value);
};
useEffect(() => {
const results: IPatient[] = cards.filter((card) =>
card.name.toLowerCase().includes(searchTerm),
);
setSearchResults(results);
}, [searchTerm, cards]);
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.