Multiple fetch data axios with React Hooks

前端 未结 4 1890
忘掉有多难
忘掉有多难 2021-01-03 13:53

I would like to get global information from Github user and his repos(and get pinned repos will be awesome). I try to make it with async await but It\'s is correct? I\'ve go

4条回答
  •  迷失自我
    2021-01-03 14:13

    function App() {
      const [resp, setGitData] = useState({ data: null, repos: null });
    
      useEffect(() => {
        const fetchData = async () => {
          const respGlobal = await axios(
            `https://api.github.com/users/${username}`
          );
          const respRepos = await axios(
            `https://api.github.com/users/${username}/repos`
          );
    
          setGitData({ data: respGlobal.data, repos: respGlobal.data });
        };
    
        fetchData();
      }, []);
    
      console.log('render');
      if (resp.data) {
        console.log("d", resp.data, resp.repos);
      }
    
      return 

    Hello

    ; } he made some mistake here: setGitData({ data: respGlobal.data, repos: respGlobal.data(respRepos.data //it should be respRepos.data});

提交回复
热议问题