简体   繁体   English

react-hooks/exhaustive-deps 导致依赖警告,修复挂起代码

[英]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.

相关问题 反应 useEffect 带有警告的钩子 react-hooks/exhaustive-deps - React useEffect hook with warning react-hooks/exhaustive-deps 带有自定义 IntersectionObserver 钩子的 react-hooks/exhaustive-deps 警告 - react-hooks/exhaustive-deps warning with custom IntersectionObserver hook 设计React Hooks可以防止React-Hooks / Exhaustive-deps警告 - Designing React Hooks prevent react-hooks/exhaustive-deps warning 如何解决`react-hooks/exhaustive-deps`的`React Hook useEffect缺少依赖`? - How to solve `React Hook useEffect has a missing dependency` of `react-hooks/exhaustive-deps`? useEffect 和 &#39;react-hooks/exhaustive-deps&#39; linting - useEffect and 'react-hooks/exhaustive-deps' linting ESLint 希望 setSate 作为 useEffect 的依赖,但这会导致无限循环(react-hooks/exhaustive-deps) - ESLint wants setSate as a dependency for useEffect but this causes an infinite loop (react-hooks/exhaustive-deps) 未找到规则“re​​act-hooks/exhaustive-deps”的定义 - Definition for rule 'react-hooks/exhaustive-deps' was not found React Hook useEffect 缺少依赖项:&#39;formValues&#39;。 包括它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: 'formValues'. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项。 包括它们或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has missing dependencies. Either include them or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:“roomID”和“sotreId”。 要么包含它们,要么删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has missing dependencies: 'roomID 'and 'sotreId'. Either include them or remove the dependency array react-hooks/exhaustive-deps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM