简体   繁体   English

我应该如何修复 eslint 警告(React Hook useEffect 缺少依赖项)和由警告引起的循环?

[英]How should I fix eslint warning (React Hook useEffect has a missing dependency) and a loop caused by the warning?

In following codes, eslint will give a warning.在下面的代码中,eslint 会给出警告。

Line 24:6:  React Hook useEffect has a missing dependency: 'fetchPosts'. Either include it or remove the dependency array  react-hooks/exhaustive-deps
import { useState, useEffect } from 'react';
import { useLocation } from "react-router-dom";
import { Layout } from './Layout';
import { TwitterPost, reloadTwitterEmbedTemplate } from '../TwitterPost';
import '../../styles/pages/TimelinePage.css'
import axios from 'axios';

export const TimelinePage = () => {
  const [posts, setPosts] = useState([]);
  const [page, setPage] = useState(1);
  const location = useLocation();

  const fetchPosts = async () => {
    const res = await axios.get('/api/posts', { params: { page: page } });
    setPosts(posts.concat(res.data));
    reloadTwitterEmbedTemplate();
    setPage(page + 1);
  };

  useEffect(() => {
    if (location.pathname !== '/') return;

    fetchPosts();
  }, [location]);

  const postTemplates = posts.map((post: any) => {
    if (post.media_name === 'twitter') return <TwitterPost mediaUserScreenName={post.media_user_screen_name} mediaPostId={post.media_post_id} />;
    return null;
  });

  return(
    <Layout body={
      <div id="timeline">
        <div>{postTemplates}</div>
        <div className="show-more-box">
          <button type="button" className="show-more-button" onClick={fetchPosts}>show more</button>
        </div>
      </div>
    } />
  );
};

I fixed the warning by adding fetchPosts .我通过添加fetchPosts修复了警告。 Then I followed eslint instructions using useCallback and adding variables used in fetchPosts to deps.然后我按照 eslint 指令使用useCallback并将fetchPosts中使用的变量添加到 deps。 This change causes a loop.此更改会导致循环。 How should I fix the loop and eslint warning?我应该如何修复循环和 eslint 警告?

import { useState, useEffect, useCallback } from 'react';
import { useLocation } from "react-router-dom";
import { Layout } from './Layout';
import { TwitterPost, reloadTwitterEmbedTemplate } from '../TwitterPost';
import '../../styles/pages/TimelinePage.css'
import axios from 'axios';

export const TimelinePage = () => {
  const [posts, setPosts] = useState([]);
  const [page, setPage] = useState(1);
  const location = useLocation();

  const fetchPosts = useCallback(async () => {
    const res = await axios.get('/api/posts', { params: { page: page } });
    setPosts(posts.concat(res.data));
    reloadTwitterEmbedTemplate();
    setPage(page + 1);
  }, [page, posts]);

  useEffect(() => {
    if (location.pathname !== '/') return;

    fetchPosts();
  }, [location, fetchPosts]);

  const postTemplates = posts.map((post: any) => {
    if (post.media_name === 'twitter') return <TwitterPost mediaUserScreenName={post.media_user_screen_name} mediaPostId={post.media_post_id} />;
    return null;
  });

  return(
    <Layout body={
      <div id="timeline">
        <div>{postTemplates}</div>
        <div className="show-more-box">
          <button type="button" className="show-more-button" onClick={fetchPosts}>show more</button>
        </div>
      </div>
    } />
  );
};

I highly recommend this article to really understand what's going on when you use the useEffect hook.我强烈推荐这篇文章来真正了解使用 useEffect 挂钩时发生了什么。 It talks, among other things, about your exact problem and ways to solve it.除其他事项外,它还讨论了您的确切问题和解决方法。 That said, you should move the function inside the useEffect callback, something like:也就是说,您应该在 useEffect 回调中移动 function ,例如:

export const TimelinePage = () => {
  /* ... */

  useEffect(() => {
    if (location.pathname !== '/') return;
    const fetchPosts = async () => {
        const res = await axios.get('/api/posts', { params: { page: page } });
        setPosts(posts.concat(res.data));
        reloadTwitterEmbedTemplate();
        setPage(page + 1);
    }
    fetchPosts();
  }, [location]);

  /* ... */
};

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

相关问题 如何修复这个“React Hook useEffect has a missing dependency”警告? - How to fix this “React Hook useEffect has a missing dependency” warning? 如何解决此警告:“React Hook useEffect 缺少依赖项:'history'”? - How to fix this warning: “React Hook useEffect has a missing dependency: 'history'”? 我应该如何删除“ React Hook useEffect缺少依赖项”警告? - How should i remove warning “React Hook useEffect has a missing dependency”? 警告:React Hook useEffect 缺少依赖项 - warning: React Hook useEffect has a missing dependency React useEffect 钩子缺少依赖项警告 - React useEffect hook has a missing dependency warning React Hook useEffect 缺少依赖项警告 - React Hook useEffect has a missing dependency warning 使用 useEffect React Hook 时如何修复缺少依赖项警告 - How to fix missing dependency warning when using useEffect React Hook React Hook useEffect 缺少依赖项:'setValid'。 如何删除此警告 - React Hook useEffect has a missing dependency: 'setValid' . How to remove this warning 使用 useEffect React Hook 时如何修复缺少依赖项警告? 我正在使用 nextjs 应用程序 - How to fix missing dependency warning when using useEffect React Hook? I am using nextjs app 在 useEffect React Hook 中缺少 object 时修复缺少的依赖警告? - fix missing dependency warning when missing an object in useEffect React Hook?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM