简体   繁体   English

Eslint hook 重构 useEffect

[英]Eslint hook refactor useEffect

I create useEffect like that.我像这样创建 useEffect 。 I want useEffect listen useToken and when I login and set token it will fetch some data我想要 useEffect 听 useToken ,当我登录并设置令牌时,它会获取一些数据

  useEffect(() => {
    if (userToken) {
      setTimeout(() => {
        fetchDataWishlistShow();
        fetchCart();
      }, 500);
    }
    fetchCategory();
  }, [userToken]);

But Eslint of hook automatic add some function to Effect但是 Eslint 的 hook 自动添加了一些 function 到 Effect

  useEffect(() => {
    if (userToken) {
      setTimeout(() => {
        fetchDataWishlistShow();
        fetchCart();
      }, 500);
    }
    fetchCategory();
  }, [fetchCart, fetchCategory, fetchDataWishlistShow, userToken]);

Why it do that.为什么这样做。 I think it wrong but anyone can explain for me?我认为这是错误的,但有人可以为我解释吗?

I guess you installed "eslint-plugin-react-hooks" and enable it.我猜你安装了“eslint-plugin-react-hooks”并启用它。

useEffect is designed the way that which ever you use in you useEffect you are recommended to add it it dependencies array list EXCEPT some things that React guarantee that they are not changed every each re-render such as: dispatch of useReducer, setState form const [state, setState] = useState() or functions or variables imported from other files. useEffect的设计方式与您在useEffect中使用的方式相同,建议您将其添加到依赖项数组列表中,除了一些 React 保证不会在每次重新渲染时更改的内容,例如:调度 useReducer、setState form const [state, setState] = useState()或从其他文件导入的函数或变量。

You can turn off the rule but you SHOULD NOT do that because you did not solve the source of the problem.您可以关闭规则,但您不应该这样做,因为您没有解决问题的根源。 Eg例如

const [cartStatus, setCartState] = useState("all") // assume it can be "all", "pending"

const fetchCart = () =>{
   fetch(`endpoint/cart/${cardStatus}`)
}

useEffect(() => {
    if (userToken) {
      setTimeout(() => {
        fetchDataWishlistShow();
        fetchCart();
      }, 500);
    }
    fetchCategory();
    // This case you alway fetch all item in cart
  }, [userToken]);

To fix issue above you saying you saying, yeah we can just add fetchCart into dependency list, but it'll cause infinite re-render, you need to wrap fetchCart by useCallback or move fetchCart into useEffect .要解决上面说的问题,是的,我们可以将fetchCart添加到依赖项列表中,但这会导致无限重新渲染,您需要通过 useCallback 包装fetchCart或将fetchCart移动到useEffect Because you call the function inside setTimeout, you might want to clean the useEffect因为您在 setTimeout 中调用了 function,所以您可能需要清理useEffect

const [cartStatus, setCartState] = useState("all") // assume it can be "all", "pending"


useEffect(() => {
   const fetchCart = () =>{
     fetch(`endpoint/cart/${cardStatus}`)
   }
   let id = null
    if (userToken) {
      id = setTimeout(() => {
        fetchDataWishlistShow();
        fetchCart();
      }, 500);
    }
    fetchCategory();
    return () => clearTimeout(id);  
  }, [userToken]);

This article is written by Dan Abramov is a good resource to look at and deep dive into how useEffect works and why you should follow the recommended way. 本文由 Dan Abramov 撰写,是一个很好的资源,可以查看并深入了解useEffect工作原理以及为什么应该遵循推荐的方式。

You might saying that "No no, I am only have api call when the component mounted, That's it".您可能会说“不不,我只有在安装组件时调用 api,就是这样”。 But when your project grown, and you components become complicated, it's hard to remember that, who's know that your requirements might be changed at some point in the future, why don't do it in proper way to give you more confident when refactor or update your components?但是当你的项目变大,你的组件变得复杂时,很难记住,谁知道你的需求在未来的某个时候可能会改变,为什么不以正确的方式去做,让你在重构或重构时更有信心更新你的组件?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM