为了账号安全,请及时绑定邮箱和手机立即绑定

如何在另一个组件 useEffect 中设置 State?

如何在另一个组件 useEffect 中设置 State?

跃然一笑 2023-09-28 16:18:59
快速查看我正在尝试做的事情:import React, { useState, useEffect } from 'react';const MainComponent = () => {  const [element, setElement] = useState([]);  const [setThings] = useSomething();  useEffect(() => {    setThings(element);  }, [element]);};const useSomething = () => {  const [things, setThings] = useState([]);  // do stuff now that you've received things  return [setThings];};以及另一个文件中的另一个钩子。这工作得很好,并且是在 ReactJS 中使用画布的绝佳解决方案。我有一个警告被抛出,我似乎无法通过我的研究来解决它:  Line 150:7:  React Hook useEffect has a missing dependency: 'setThings'. Either include it or remove the dependency array  react-hooks/exhaustive-deps在反应文档中,它说这会导致问题,因为它正在调用具有依赖性的东西,但我不知道它们在哪里?
查看完整描述

1 回答

?
守着星空守着你

TA贡献1799条经验 获得超8个赞

尝试添加setThings到 中的依赖项数组useEffect

useEffect(() => {
  setThings(element);
}, [element, setThings]);


查看完整回答
反对 回复 2023-09-28
  • 1 回答
  • 0 关注
  • 56 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信