简体   繁体   English

React Hook useEffect 缺少依赖项:'data'。 包括它或删除依赖数组

[英]React Hook useEffect has a missing dependency: 'data'. Either include it or remove the dependency array

I have a filter function which is filtering data with state data, dataCopy and searchValue.我有一个过滤器 function,它使用 state 数据、dataCopy 和 searchValue 过滤数据。 Issue is if i don't include the data state than react gives warning and if i do include it it cause infinite loop cause the data array is changing within the useEffect.问题是如果我不包含数据 state 而不是反应发出警告,如果我包含它会导致无限循环,导致数据数组在 useEffect 中发生变化。 How can i make so that i don't get that warning.我怎样才能让我没有得到那个警告。

Filter function过滤器 function

import React, { useEffect, useState } from 'react'
import Header from '../Components/Header/Header'
import Home from '../Components/Home/Home'
import "./Layout.css"
import Spinner from '../Components/Spinner/Spinner'

function Layout() {

    // state for data, copy of data and spinner
    const [data, setData] = useState([])
    const [dataCopy, setDataCopy] = useState([])

    // state for search input in Header.js (define in parent )
    const [searchValue, setSearchValue] = useState("")

    // changing search value
    const changeSearchValue = (value) => {
        setSearchValue(value)
    }


    // useEffect for search functionality
    useEffect(() => {
        const handleSearch = () => {
            if (searchValue !== "") {
                
                const searchFilter = data.filter(item =>
                    !isNaN(searchValue) ? item.expected_annually_bill_amount.toString().includes(searchValue) :
                        item.dmo_content.Ausgrid.toString().toLowerCase().includes(searchValue.toLowerCase()))
                setData(searchFilter)
            } else {
                setData(dataCopy)
            }

        }
        handleSearch()
    }, [searchValue, dataCopy])





    // useEffect for getting data from api
    useEffect(() => {
        // making post request to get the token
        axios.post(`${process.env.REACT_APP_BASE_URL}`, { data: "" },
            {
                headers:
                {
                    'Api-key': `${process.env.REACT_APP_API_KEY}`,
                },
            })
            // after getting to token returning it for callback
            .then((response) => {
                return response.data.data.token
            })
            // using the token to call another api for the needed data
            .then((tokenIs) => {
                axios.post(`${process.env.REACT_APP_DATA_URL}`,
                    { "session_id": `${process.env.REACT_APP_SESSION_ID}` },
                    {
                        headers:
                        {
                            'Api-key': `${process.env.REACT_APP_API_KEY}`,
                            'Auth-token': tokenIs,
                        },
                    })
                    .then((response) => {
                        setData(response.data.data.electricity)
                        setDataCopy(response.data.data.electricity)
                        setSpinner(false)
                    })
            })
            // catiching any error if happens
            .catch((err) => {
                setSpinner(false)
                alert(err)
            })

    }, [])


    return (<>
       
            <div className='layout'>
                <Header
                    changeSearchValue={changeSearchValue}
                    searchValue={searchValue}
                />
                <Home data={data} />
            </div>
        
    
    )
}

export default Layout

Here you can eliminate data dependency by:在这里,您可以通过以下方式消除data依赖性:

useEffect(() => {
        const handleSearch = () => {
            if (searchValue !== "") {
                setData(data => data.filter(item =>
                    !isNaN(searchValue) ? item.expected_annually_bill_amount.toString().includes(searchValue) :
                        item.dmo_content.Ausgrid.toString().toLowerCase().includes(searchValue.toLowerCase())))
            } else {
                setData(dataCopy)
            }

        }
        handleSearch()
    }, [searchValue, dataCopy])

You can add the following comment above the dependency array for suppressing the warning您可以在依赖数组上方添加以下注释以抑制警告

// eslint-disable-next-line react-hooks/exhaustive-deps

暂无
暂无

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

相关问题 React Hook useEffect 缺少依赖项:&#39;formData&#39;。 包括它或删除依赖项数组。 什么是依赖就是使用 - React Hook useEffect has a missing dependency: 'formData'. Either include it or remove the dependency array. what is dependency is use React Hook useEffect 缺少依赖项:'props'。 包括它或删除依赖项数组。 useEffect 中的道具没有数据 - React Hook useEffect has a missing dependency: 'props'. Either include it or remove the dependency array. props in useEffect have no data React Hook useEffect 缺少一个依赖项:'handleLogout'。 要么包含它,要么移除依赖数组 react - React Hook useEffect has a missing dependency: 'handleLogout'. Either include it or remove the dependency array react React Hook React.useEffect 缺少依赖项:“loadData”。 包含它或删除依赖项数组 - React Hook React.useEffect has a missing dependency: 'loadData'. Either include it or remove the dependency array React Hook useEffect 缺少依赖项:&#39;context&#39;。 包括它或删除依赖项数组 - React Hook useEffect has a missing dependency: 'context'. Either include it or remove the dependency array React Hook useEffect 缺少依赖项:&#39;fetchTracker&#39;。 包括它或删除依赖项数组 - React Hook useEffect has a missing dependency: 'fetchTracker'. Either include it or remove the dependency array 如何解决“React Hook useEffect 缺少依赖项。包含它或删除依赖项数组”的问题? - How to fix "React Hook useEffect has a missing dependency. Either include it or remove the dependency array" problem? React Hook useEffect 缺少依赖项:'id'。 包括它或删除依赖数组 - React Hook useEffect has a missing dependency: 'id'. Either include it or remove the dependency array React Hook useEffect 缺少一个依赖项:'tasks'。 包括它或删除依赖数组 - React Hook useEffect has a missing dependency: 'tasks'. Either include it or remove the dependency array React Hook useEffect 缺少依赖项:'fetchComments'。 包括它或删除依赖数组 - React Hook useEffect has a missing dependency: 'fetchComments'. Either include it or remove the dependency array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM