简体   繁体   English

React Hook useEffect 缺少依赖项:'props'

[英]React Hook useEffect has a missing dependency: 'props'

I am trying to use react hooks (this is my first time) but I get this error when using useEffect like the above componentDidMount: 'React Hook useEffect has a missing dependency: 'props'.我正在尝试使用反应钩子(这是我的第一次),但是在使用像上面的 componentDidMount 一样的 useEffect 时出现此错误:'React Hook useEffect has a missing dependency:'props'。 Either include it or remove the dependency array.包括它或删除依赖项数组。 However, 'props' will change when any prop changes, so the preferred fix is to destructure the 'props' object outside of the useEffect call and refer to those specific props inside useEffect'然而,'props' 会在任何props 改变时改变,所以首选的解决方法是在 useEffect 调用之外解构 'props' 对象,并在 useEffect' 中引用那些特定的 props'

I'm trying to make a GET request to an API, and that needs some props in the url.我正在尝试向 API 发出 GET 请求,这需要 url 中的一些道具。 This is my code:这是我的代码:

 function SomeComponent(props) { const [data, setData] = useState({}); const [loading, setLoading] = useState(true); const [hasError, setErrors] = useState(false); useEffect(() => { async function fetchData() { try { let response = await Axios.get( `/some-url/${props.obj.id}/abc`, ); let { data } = response.data; setData(data); setLoading(false); props.totalStats({data }); } catch (error) { setErrors(error); setLoading(false); } } fetchData(); }, []); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

I've been looking and tried these solutions, but they don't work for me:我一直在寻找并尝试过这些解决方案,但它们对我不起作用:

 const { id } = props.obj.id; const {totalStats} = props.totalStats useEffect(()=>{ //Here is the code //I replace the $ {props.obj.id} by the id and the function props.totalStats by //totalStats },[id,totalStats]

Doing it that way I obtained an undefined in the id and in the call to the function.这样做我在 id 和对函数的调用中获得了一个 undefined 。

Then I tried doing it this way:然后我尝试这样做:

 useEffect(()=>{ //here is the code like the first one },[props])

But this makes n number of requests to the api但这对 api 产生了 n 个请求

Remove the braces from the constant imports.从常量导入中删除大括号。

So it should look something like this:所以它应该看起来像这样:

function SomeComponent(props) {
  const objId = props.obj.id;
  const totalStats = props.totalStats;
  
  const [data, setData] = useState({});
  const [loading, setLoading] = useState(true);
  const [hasError, setErrors] = useState(false);
  
  useEffect(() => {
    async function fetchData() {
      try {
        let response = await Axios.get( `/some-url/${objId}/abc`,
          
        );
        let { data } = response.data;
        setData(data);
        setLoading(false);
        totalStats({data });
      } catch (error) {
        setErrors(error);
        setLoading(false);
      }
    }
    fetchData();
  }, [objId, totalStats]);
}

The answer is accepted already but just to clarify why OP's solution didn't work in the begining is not about assigning consts.答案已经被接受,但只是为了澄清为什么 OP 的解决方案在开始时不起作用与分配常量无关。 It is because OP made a mistake to destructure the properties.这是因为OP在破坏属性方面犯了一个错误。 Correct way to do it:正确的做法:

const { id } = props.obj;
const {totalStats} = props

Notice that I didn't put props.object.id or props.totalStats .请注意,我没有放置props.object.idprops.totalStats Because if you put these you are actually trying to destructure id from props.object.id object.因为如果你把这些你实际上是在试图从props.object.id对象中解构id And totalStats from props.totalStats .totalStatsprops.totalStats Which there weren't such properties so OP got the undefined error.没有这样的属性,所以 OP 得到了未定义的错误。

You can also assign a constant from the props object as the accepted answer's way.您还可以从 props 对象分配一个常量作为接受答案的方式。 This is practically the same thing.这实际上是同一件事。

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

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