从另一个组件获取的正确方法 - ReactJS

问题描述 投票:0回答:2

我有一个可以正确获取数据的组件,但我想将其封装在一个助手中。我尝试了很多事情,但我被困住了。

这是有效的组件:

export const Carousel = () => {

const [ lotteries, setLotteries ] = useState({});
const [ isLoading, setisLoading ] = useState(true);

useEffect(() => {
    
    async function fetchAPI() {
        const url = 'https://protected-sea-30988.herokuapp.com/https://www.lottoland.com/api/drawings;'
        let response = await fetch(url)
        response = await response.json()
        
        setLotteries(response)
        setisLoading(false)
        }
    
        fetchAPI()
}, [])

return (

    <>
        {
            isLoading ? (
                <span>loading</span>
            ) : (

            <Slider >
                {
                Object.keys(lotteries).map((lottery, idx) => {
                    return (
                        <Slide 
                            key={ idx }
                            title={ lottery }
                            prize={ lotteries[lottery].next.jackpot }
                            day={ lotteries[lottery].next.date.day }
                        />
                    )
                })
                }
            </Slider>
        )}
    </>
);}

这是我迄今为止尝试的最后一件事。这是没有 fetch 的组件

export const Carousel = () => {

const [ lotteries, setLotteries ] = useState({});
const [ isLoading, setIsLoading ] = useState(true);

useEffect(() => {
    
    getLotteries()
    setLotteries(response)
    setIsLoading(false)
      
}, [])

这就是我尝试封装抓取的地方。

export const getLotteries = async() => {

    const url = 'https://protected-sea-30988.herokuapp.com/https://www.lottoland.com/api/drawings;'
    let response = await fetch(url)
    response = await response.json()

    return response;
    
}

我对 React 有点陌生,所以任何帮助将不胜感激。非常感谢。

javascript reactjs fetch-api
2个回答
1
投票

要从 getLotteries 助手获取获取的数据,您必须返回一个承诺

export const getLotteries = async() => {
const url = 'https://protected-sea- 
 30988.herokuapp.com/https://www.lottoland.com/api/drawings;'
let response = await fetch(url)
return  response.json()

}

并将其称为 async/await

useEffect(async() => {

let response= await getLotteries()
setLotteries(response)
setIsLoading(false)

}, [])

1
投票

如果您想将请求 URL 的逻辑分离到另一个辅助函数中,您可以创建一个自定义挂钩。

// customHook.js

import { useEffect, useState } from 'react';

export function useLotteries() {
  const [lotteries, setLotteries] = useState(null);

  useEffect(() => {
    fetch('https://protected-sea-30988.herokuapp.com/https://www.lottoland.com/api/drawings;')
      .then(response => response.json())
      .then(json => setLotteries(json));
  }, []);

  return lotteries;
}

// Carousel.js

import { useLotteries } from "./customHook.js";

export const Carousel = () => {
  const lotteries = useLotteries();

  if (lotteries) {
    return; /* Your JSX here! (`lotteries` is now contains all the request responses) */
  } else {
    return <Loader />; // Or just null if you don't want to show a loading indicator when your data hasn't been received yet.
  }
};

© www.soinside.com 2019 - 2024. All rights reserved.