React useEffect hook 和 Async/await 自己的获取数据功能?

IT技术 javascript reactjs asynchronous async-await react-hooks
2021-05-19 23:46:55

我试图创建一个从服务器获取数据的函数,它工作正常。但我不确定这是否正确?

我创建了一个函数组件来获取数据,使用useStateuseEffectAsync/ Await

import React, { useState, useEffect } from "react";

const Fetch = () => {
  const [data, setData] = useState(null);
  useEffect(() => {
    const fetchData = async () => {
      let res = await fetch(
        "https://api.coindesk.com/v1/bpi/currentprice.json" //example and simple data
      );
      let response = await res.json();
      setData(response.disclaimer); // parse json
      console.log(response);
    };
    fetchData();
  }, []);
  return <div>{data}</div>;
};

export default Fetch; // don't run code snippet, not working, this component must be imported in main

在哪里我不知道是一个地方在那里调用fetchData功能。我在useEffect里面做那个?正确的位置?并且,这个调用只会发生一次?因为我使用[]

一般来说,你会怎么做这样的事情?

2个回答

总的来说,您正朝着正确的方向前进。为了获取数据,您需要使用useEffect[]作为第二个参数传递以确保它仅在初始安装时触发。

我相信您可以从解耦fetchJson函数中受益并使其更通用,例如:

const fetchJson = async (url) => {
  const response = await fetch(url);
  return response.json();
};

const Fetch = () => {
  const [data, setData] = useState(null);

  useEffect(() => {
    fetchJson("https://api.coindesk.com/v1/bpi/currentprice.json")
      .then(({ disclaimer }) => setData(disclaimer));
  }, []);

  return <div>{data}</div>;
};

另一种选择是使用自调用函数:

const Fetch = () => {
  const [data, setData] = useState(null);
  useEffect(() => {
    (async () => {
      let res = await fetch(
        "https://api.coindesk.com/v1/bpi/currentprice.json" //example and simple data
      );
      let response = await res.json();
      setData(response);
    })();
  }, []);
  return <div>{data}</div>;
};

将 fetch 逻辑分离到一个单独的函数的建议是一个好主意,可以按如下方式完成:

const Fetch = () => {
  const [data, setData] = useState(null);
  useEffect(() => {
    (async () => {
      let response= await fetchData("https://api.coindesk.com/v1/bpi/currentprice.json");
      setData(response);
    })();
  }, []);
  return <div>{data}</div>;
};

const fetchData = async (url) => {
  const response = await fetch(url);
  const json = await response.json();

  return json;
};

还有一个选择是围绕 useEffect 创建一个包装函数,它会为您触发异步函数,类似于:

export function useAsyncEffect(effect: () => Promise<any>) {
  useEffect(() => {
    effect().catch(e => console.warn("useAsyncEffect error", e));
  });
}