简体   繁体   English

React Hook useEffect 缺少依赖项要么包含它,要么删除依赖项数组 react-hooks/exhaustive-deps

[英]React Hook useEffect has a missing dependency Either include it or remove the dependency array react-hooks/exhaustive-deps

My react app gives me this warning:我的反应应用程序给了我这个警告:

React Hook useEffect has a missing dependency Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项要么包含它,要么删除依赖项数组 react-hooks/exhaustive-deps

If I move my Stateful components to the inside of the useEffect hook, then my Buttons can't access setButton anymore.如果我将 Stateful 组件移动到 useEffect 钩子的内部,那么我的 Button 将无法再访问 setButton。 Is there any way to fix this issue?有没有办法解决这个问题?

  const [value, setValue] = useState([])
  const [isLoading, setLoading] = useState(false)
  const [buttonValue, setButton] = useState(false)

This is my useEffect Hook这是我的 useEffect Hook

  useEffect(() => {
    axios.get('http://localhost:5000/').then(res => setValue(res.data))

    if (buttonValue === 'delete') {
      setLoading(true)
      axios.delete('http://localhost:5000/delete').then(() => {
        setLoading(false)
        setButton(null)
      })
    } else if (buttonValue === 'create') {
      setLoading(true)
      axios.post('http://localhost:5000/').then(() => {
        setLoading(false)
        setButton(null)
      })
    }
  }, [isLoading])

These are my Buttons这些是我的按钮

   <Form>
     <Form.Group>
       <Button variant='success' className='button' onClick={() => setButton('create')} id='create'>Create Sales Order</Button>
     </Form.Group>
     <Form.Group>
       <Button variant='danger' className='button' onClick={() => setButton('delete')} id='delete'>Delete Sales Order</Button>
     </Form.Group>
   </Form>

useEffect will re-run the effect callback whenever any of the items in dependency array changes (eg loading goes from false to true).每当依赖数组中的任何项目发生更改(例如加载从 false 变为 true)时,useEffect 将重新运行效果回调。

The warning you are receiving is because you have used buttonValue in your effect callback but not included in the dependency array.您收到的警告是因为您在效果回调中使用了buttonValue但未包含在依赖项数组中。 So when you call the setButton() in click handler of the button, nothing happens because the useEffect won't detect any change to re-run the effect callback.因此,当您在按钮的单击处理程序中调用setButton()时,没有任何反应,因为 useEffect 不会检测到任何更改以重新运行效果回调。

However, I suggest that you change the implementation, useEffect should be used for Side Effects (eg triggering a list update when the user selects a dropdown), not the effect themselves (Clicking on a submit buttons).但是,我建议您更改实现,useEffect 应该用于副作用(例如,当用户选择下拉列表时触发列表更新),而不是效果本身(单击提交按钮)。

I would rewrite the code like this:我会像这样重写代码:

  const [value, setValue] = useState([]);
  const [isLoading, setLoading] = useState(false);

  useEffect(() => {
    axios.get('http://localhost:5000/').then(res => setValue(res.data))
  }, [])


  const handleCreateClick = () => {
      setLoading(true);
      axios.post('http://localhost:5000/create').then(() => {
        setLoading(false);
      });
  }

  const handleDeleteClick = () => {
      setLoading(true);
      axios.delete('http://localhost:5000/delete').then(() => {
        setLoading(false);
      });
  }


  return (
        <Form>
          <Form.Group>
             <Button variant='success' className='button' onClick={handleCreateClick} id='create'>Create Sales Order</Button>
          </Form.Group>
          <Form.Group>
             <Button variant='danger' className='button' onClick={handleDeleteClick} id='delete'>Delete Sales Order</Button>
          </Form.Group>
        </Form>
  );

The approach above is more readable and less error-prone as we are separating the effects from side-effects.由于我们将影响与副作用分开,因此上述方法更具可读性且不易出错。

暂无
暂无

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

相关问题 React Hook useEffect 缺少依赖项:&#39;user.id&#39;。 包括它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: 'user.id'. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项。 要么包含它,要么移除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:“url”。 包括它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: 'url'. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:&#39;dispatch&#39;。 包括它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: 'dispatch'. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:&#39;fetchProfile&#39;。 包括它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: 'fetchProfile'. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:&#39;formValues&#39;。 包括它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: 'formValues'. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:包含它或删除依赖项数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: Either include it or remove the dependency array react-hooks/exhaustive-deps 第 93:6 行:React Hook useEffect 缺少依赖项:'estado'。 要么包含它,要么移除依赖数组 react-hooks/exhaustive-deps - Line 93:6: React Hook useEffect has a missing dependency: 'estado'. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:'loading'。 包括它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: 'loading'. Either include it or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:“loadAllProducts”。 包括它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: 'loadAllProducts'. Either include it or remove the dependency array react-hooks/exhaustive-deps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM