简体   繁体   English

在响应中添加 useEffect 的正确方法来调用一次以满足 eslint react-hooks/exhaustive-deps?

[英]Proper way to add useEffect in react to call once to satisfy eslint react-hooks/exhaustive-deps?

I'm using eslint react-hooks/exhaustive-deps the problem I'm having is my component needs to call only once regardless of changes on props.我正在使用 eslint react-hooks/exhaustive-deps我遇到的问题是我的组件只需要调用一次,而不管道具的变化。 It is fine with [] but when using dependencies it is called multiple time. []很好,但是当使用依赖项时,它会被多次调用。

my code is (with some omitted)我的代码是(省略了一些)

  • parents父母
// some logic for tracking change in realtime
<Component trackingCode=tracking />
  • children:孩子们:
export default function Component(props){
  // if I use depedencies, it will called multiple times because 
  // trackingCode keep changing
  React.useEffect(()=>{
    // some code
    // fetch function
   const _updateSession = async () => {
      const tracking = props.trackingObject;

      const formData = new URLSearchParams();
      formData.append('trackingCode', tracking.trackingCode);
      formData.append('sessionCode', tracking.sessionCode);
      formData.append('content', trackingStage);

      try {
        const req = await fetch(`https://example.com/tracking/${tracking.id}/`, {
          method: 'PUT',
          body: formData,
        })
        const res = await req.json();

        switch (req.status) {
          case 200:
            props.setQuestion('trackingObject', res);
            break
        }

      } catch (err) {
        console.log(err)
      }
    }
   _updateSession()
  },[props.trackingCode]) 
  // if props.trackingCode is ommited like so [] it works just fine
 

  return (
  <>
    // some code 
  </>)
}

I tried to use useRef but somehow the trackingcode is empty.我尝试使用useRef但不知何故跟踪代码为空。 Is there a way to satisfy the dependencies without making it call multiple times?有没有办法在不多次调用的情况下满足依赖关系?

thanks谢谢

Proper way is that if you want to execute the useEffect at once just don't pass the dependencies in in dependency array.正确的方法是,如果你想立即执行 useEffect 只是不要在依赖数组中传递依赖项。 I know you mentioned it but its right way.我知道你提到了它,但它是正确的方式。 If you want to run useEffect on state change then pass the state variable in dependency array.如果要在 state 更改上运行 useEffect,则在依赖数组中传递 state 变量。

React.useEffect(()=>{
    // some code
    // fetch function
   const _updateSession = async () => {
      const tracking = props.trackingObject;

      const formData = new URLSearchParams();
      formData.append('trackingCode', tracking.trackingCode);
      formData.append('sessionCode', tracking.sessionCode);
      formData.append('content', trackingStage);

      try {
        const req = await fetch(`https://example.com/tracking/${tracking.id}/`, {
          method: 'PUT',
          body: formData,
        })
        const res = await req.json();

        switch (req.status) {
          case 200:
            props.setQuestion('trackingObject', res);
            break
        }

      } catch (err) {
        console.log(err)
      }
    }
   _updateSession()
  },[]) 

暂无
暂无

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

相关问题 反应 useEffect 带有警告的钩子 react-hooks/exhaustive-deps - React useEffect hook with warning react-hooks/exhaustive-deps useEffect 和 &#39;react-hooks/exhaustive-deps&#39; linting - useEffect and 'react-hooks/exhaustive-deps' linting 了解 react-hooks/exhaustive-deps useEffect 和调度事件 - understanding react-hooks/exhaustive-deps useEffect and dispatching events 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) 我如何正确使用 useEffect 进行异步获取调用和反应? 反应钩子/详尽的deps - How do i properly use useEffect for a async fetch call with react? react-hooks/exhaustive-deps 解决 react-hooks/exhaustive-deps 的最佳方法 - Best way to resolve react-hooks/exhaustive-deps 解决 react-hooks/exhaustive-deps 警告的最佳方法 - Best way to resolve react-hooks/exhaustive-deps warning eslint-plugin-react-hooks 出现意外错误(react-hooks/exhaustive-deps) - eslint-plugin-react-hooks is giving unexpected errors (react-hooks/exhaustive-deps) React Hooks react-hooks/exhaustive-deps eslint 规则似乎过于敏感 - React Hooks react-hooks/exhaustive-deps eslint rule seems overly sensitive 如何在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”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM