【问题标题】:React problem hook useeffect return first element emptyReact 问题钩子 useEffect 返回第一个元素为空
【发布时间】:2022-06-23 01:20:22
【问题描述】:

useEffect axios 调用返回一个空数组,后跟预期结果

const useGetProducts = (API) => {
  const [products, setProducts] = useState([])

  useEffect(() => {
    async function fetchData() {
      const response = await axios(API);
      setProducts(response.data)
    }
    fetchData();
  }, []);

  return products;
}

因此,当尝试使用 axios 调用的结果时,我得到一个未定义的错误导致。

<ProductItem product={product} />

【问题讨论】:

  • 你是如何在 ` 组件中使用你的数据的?

标签: reactjs


【解决方案1】:

Erm full nooby 在这里,但是原始 axios() 到底在做什么?为什么不用 axios.get() 或 smth?

【讨论】:

  • axios() 有更一般的用法
  • 谢谢,刚刚意识到!
猜你喜欢
  • 2019-12-09
  • 2022-07-04
  • 2020-12-20
  • 1970-01-01
  • 2019-08-09
  • 1970-01-01
  • 1970-01-01
  • 2020-07-13
  • 2020-09-20
相关资源
最近更新 更多