React Hook useEffect缺少依赖关系(未将函数移入useEffect中)

4
这是我的代码部分。一切都正常工作,但ESLint会提示错误。

React Hook useEffect缺少依赖项:'dispatch'和'getData'。请在依赖项数组中包含它们或删除该依赖 react-hooks/exhaustive-deps

我找到了一些解决方案,但它们都说你需要将函数移动到useEffect中,但我无法这样做。因为有时我正在调用位于Jsx中的setData()函数。
因此,getData不仅在组件挂载时运行。
有类似的问题,但正如我所说;我不能将函数移动到useEffect中。答案总是相同: React Hook useEffect缺少依赖项 React Hook useEffect缺少依赖项:'list'
export const Complex = (props) => {
// [+] Olmayan başlık tespiti. [+] Var olan başlık tespiti. [-] Daha önce
// açılmış fakat ilk entrysi silinmiş başlıklar.
const params = queryString.parse(props.location.search)

let id = props.location.pathname.split("--")[1];
let str = props.location.pathname.split("--")[0].substr(1);

const data = {
    id: id,
    link: str,
    page: params.sayfa ? parseInt(params.sayfa) : 1,
    isGood: params.guzel ? params.guzel : false
};


// STATE
const [title,setTitle] = useState("")
const [titleSubs,setTitleSubs] = useState("")
const [entryCount,setEntryCount] = useState()
const [titleId,setTitleID] = useState()
const [exist,setExist] = useState(true)
const [entries,setEntries] = useState([])
const [likes,setLikes] = useState([])
const [disabled,setDisable] = useState(false)
const [isLoading,setLoading] = useState(false)

// REDUX
const titleModal = useSelector(state => state.settings.titleModal)
const dataPage = useSelector(state => state.pageInfo.page)
const entryNormal = useSelector(state => state.character.entry.entryNormal)
const entryCats = useSelector(state => state.character.entryCats)
const isAuthenticated = useSelector(state => state.auth.authenticated)
const dispatch = useDispatch();

function getData() {
    setLoading(true)
    // For everyone
    axios.post('/api/data/entry/get', data)
    .then(res  => {
            setExist(true)
            setTitleID(res.data.title.id)
            setEntries(res.data.entries)
            setEntryCount(res.data.count)
            setTitle(res.data.title)
            setTitleSubs(res.data.title.titlesubs)
            setLoading(false)

            if (titleModal) {
                // Send Redux Link Informations
                dispatch(setCurrentPage({type: null, id: null, title: null}))
            } else {
                dispatch(setCurrentPage({type: "entry", id: res.data.title.id, title: res.data.title.title}))
            }
    })
    .catch(err => {
        console.log(err)
        setExist(false)
        setLoading(false)
        setTitle(str)
    })

    // If Authenticated.
    // Get liked entries.
    if (isAuthenticated) {
        axios.post('api/data/entry/likes/get', {titleId: data.id})
        .then(res => {
            const LikesList = [];
            res.data.forEach(data => {
                LikesList.push(data.EntryId)
            });
            setLikes(LikesList)
        })
    }

}

useEffect(() => {
    getData()
    return () => {
        setEntries([])
        dispatch(setCurrentPage({type: null, id: null, title: null}))
    }
}, [id, props.location.search])

function getGoodEntriesGeneral() {
    const params = queryString.parse(props.location.search)
    params['guzel'] = true;
    const serialize = obj => Object.keys(obj)
                         .map(key => `${key}=${encodeURIComponent(obj[key])}`).join('&')
    history.push({
        pathname: props.location.pathname,
        search: serialize(params)
    })
}

1
你在 axios.post 中使用的 data 是来自状态(state)吗? - Asaf Aviv
@AsafAviv 我更新了问题。 - mehmetdemiray
1个回答

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接