警告React Hook useEffect缺少依赖项

时间:2020-10-27 23:11:23

标签: reactjs react-hooks

如果有人可以帮助,我想摆脱这个警告

React Hook has a missing dependency 'client' either include or remove it

当我将client传递给钩子第二个参数(数组)时,它开始进行无限循环,当我完全删除数组时,同样的事情,这是我的代码:

 `const [products, setProducts] = useState({})

  const client = createClient({
    // This is the space ID. A space is like a project folder in 
       Contentful terms
    space: "lku7p...spaceID",
    // This is the access token for this space. Normally you get both 
       ID and the token in the Contentful web app
    accessToken: "6uTK1K_DfE5uAn4.....my Key"
  })`

useEffect(()=>{
   client.getEntries()
    .then((res) => {
    let result = res.items
    result = result.map(item=>{
        const id = item.sys.id
    return {id}
    })
    setProduct(result)
},[])

0 个答案:

没有答案