简体   繁体   English

React Hooks react-hooks/exhaustive-deps eslint 规则似乎过于敏感

[英]React Hooks react-hooks/exhaustive-deps eslint rule seems overly sensitive

I am having trouble completely fully understanding dependencies in the linter rule react-hooks/exhaustive-deps.我无法完全理解 linter 规则 react-hooks/exhaustive-deps 中的依赖关系。 It seems to me that in many situations you do not need a specific variable in the array because you do not want the hook to be called when that variable changes.在我看来,在许多情况下,您不需要数组中的特定变量,因为您不希望在该变量更改时调用钩子。 However, this linting rule will tell you that it is missing from the dependencies if it is inside of the hook.但是,这个 linting 规则会告诉你,如果它在钩子内部,它就会从依赖项中丢失。 I understand that I can disable the line, but I am wondering if there is something I am misunderstanding about the rule, and if there would be a better way to code this functionality using hooks.我知道我可以禁用该行,但我想知道我是否对规则有误解,以及是否有更好的方法来使用钩子编写此功能。 Here is an example of code that I feel this rule does not need to be applied.这是我认为不需要应用此规则的代码示例。

I have the following two state variables:我有以下两个状态变量:

const [taggedUsers, setTaggedUsers] = useState([])
const [users, setUsers] = useState([])

taggedUsers represent the users who are associated with an entity (of which only the emails strings of users are saved to), whereas users are user objects fetched from another api which have more information than just the emails of users. taggedUsers 代表与实体关联的用户(其中​​仅保存用户的电子邮件字符串),而用户是从另一个 api 获取的用户对象,该对象具有比用户电子邮件更多的信息。

I have a component with a hook that hydrates taggedUser objects when called so I can display their names on the page.我有一个带有钩子的组件,可以在调用时对 taggedUser 对象进行水合,以便我可以在页面上显示它们的名称。 When I load the page, the endpoint I hit that gives me the entity has a property taggedUsers which is only a list of emails associated with the entity.当我加载页面时,我点击的给我实体的端点有一个属性 taggedUsers ,它只是与实体关联的电子邮件列表。 I have a transformer that takes those emails and maps them to a list of taggedUser objects that look like this:我有一个转换器,它接收这些电子邮件并将它们映射到如下所示的 taggedUser 对象列表:

{ email: "john.smith@gmail.com", name: null, id: "john.smith@gmail.com" }

After hydration, the object will look like this:水化后,物体将如下所示:

{ email: "john.smith@gmail.com", name: "John Smith", id: "john.smith@gmail.com" }

I have an endpoint that I can hit to get a list of users, and I use the data fetched from there to hydrate the names of the user objects.我有一个端点,我可以点击它来获取用户列表,我使用从那里获取的数据来合并用户对象的名称。 When my component first renders, I fetch the users, and set the state of my variable "users" to those users.当我的组件第一次呈现时,我获取用户,并将我的变量“用户”的状态设置为这些用户。 The change in users then triggers the next hook:用户的变化然后触发下一个钩子:

useEffect(() => {
const hydrateTaggedUsers = () => {
  const hydratedTaggedUsers = []
  users.forEach((user) => {
    taggedUsers.forEach((taggedUser) => {
      if (user.email === taggedUser.email) hydratedTaggedUsers.push(user)
    })
  })
  setTaggedUsers(hydratedTaggedUsers)
}

if (users.length) hydrateTaggedUsers()
}, [users])

I only want this hook to be called once when "users" changes, which is only once per page load.我只希望在“用户”更改时调用此挂钩一次,每次加载页面仅调用一次。 According to the linter setting, taggedUsers is missing from the dependencies of the hook.根据 linter 设置,钩子的依赖项中缺少 taggedUsers。 However, I do not want this hook to be called upon the value of taggedUsers changing, and adding it as a dependency causes a stack overflow because this hook changes the value of taggedUsers.但是,我不希望在 taggedUsers 的值发生变化时调用此钩子,将其添加为依赖项会导致堆栈溢出,因为此钩子更改了 taggedUsers 的值。

Is there something I am missing about how hooks should be constructed?关于钩子应该如何构造,我有什么遗漏吗? It feels to me that since I do not want this hook to be called upon the value of taggedUsers being updated, it should not be included in the dependency array.在我看来,由于我不希望在更新 taggedUsers 的值时调用此钩子,因此不应将其包含在依赖项数组中。 However, the linter still wants it to be in there.但是,linter 仍然希望它在那里。 Is this a situation where disabling the line is just inevitable?在这种情况下,禁用线路是不可避免的吗?

Try something..尝试一些..

Did you have the full code so I can check this deeper?你有完整的代码所以我可以更深入地检查一下吗?

I suggest this, not sure that its work :)我建议这样做,不确定它是否有效:)

useEffect(() => {
 if (users.length) {
   const hydratedTaggedUsers = [];
   users.forEach(user => {
     taggedUsers.forEach(taggedUser => {
       if (user.email === taggedUser.email) hydratedTaggedUsers.push(user);
     });
   });
   setTaggedUsers(hydratedTaggedUsers);
 }
}, [users]);

暂无
暂无

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

相关问题 如何在React中使用钩子实现componentDidMount以使其符合EsLint规则“ react-hooks / exhaustive-deps”:“ warn”? - How to implement componentDidMount with hooks in React to be in line with the EsLint rule “react-hooks/exhaustive-deps”: “warn”? 未找到规则“re​​act-hooks/exhaustive-deps”的定义 - Definition for rule 'react-hooks/exhaustive-deps' was not found 反应 useEffect 带有警告的钩子 react-hooks/exhaustive-deps - React useEffect hook with warning react-hooks/exhaustive-deps useEffect 和 'react-hooks/exhaustive-deps' linting - useEffect and 'react-hooks/exhaustive-deps' linting 带有自定义 IntersectionObserver 钩子的 react-hooks/exhaustive-deps 警告 - react-hooks/exhaustive-deps warning with custom IntersectionObserver hook 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) React挂钩:如何使用react-hooks / exhaustive-deps规则在没有无限循环的情况下读取和更新挂钩中的状态 - React hooks: How to read & update state in hooks without infinite loops with react-hooks/exhaustive-deps rule 设计React Hooks可以防止React-Hooks / Exhaustive-deps警告 - Designing React Hooks prevent react-hooks/exhaustive-deps warning 我如何正确使用 useEffect 进行异步获取调用和反应? 反应钩子/详尽的deps - How do i properly use useEffect for a async fetch call with react? react-hooks/exhaustive-deps 如何解决`react-hooks/exhaustive-deps`的`React Hook useEffect缺少依赖`? - How to solve `React Hook useEffect has a missing dependency` of `react-hooks/exhaustive-deps`?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM