Giter VIP home page Giter VIP logo

Comments (9)

alex-cory avatar alex-cory commented on May 15, 2024

I'm assuming you're talking about pagination? You could do something like

const SomeComp = ({ page, pageLength }) => {
  const [list, setList] = useState([])
  const { get, loading } = useFetch('url')

  const fetchList = async () => {
    const theList = await get(`?page=${page}&pageLength=${pageLength}`)
    setList(theList)
  }

  const mounted = useRef(false)
  useEffect(() => { // componentDidMount
    if (!mounted.current) {
      fetchList()
      mounted.current = true
    }
  }, [])

  return <div>{list.map(item => <div>{item.text}</div>)}<div>
}

Does this make sense? Or are you asking something else?

from use-http.

supermihi avatar supermihi commented on May 15, 2024

Sorry for being unclear. What I actually am trying to do is load multiple URLs at once:

const SomeComp = ({urls}) => {
  const images = // fetch all the urls
  return images.map(/* do something with the results*/)
}

However I understand now that I can probably solve it like this:

const [results, setResults] = useState([])
const request = useFetch()
async function fetchStuff() {
    for (const url of urls) {
        const result = await request.get(url)
        setResults([...results, result])
    }
  }
useEffect(() => { fetchStuff() })

from use-http.

alex-cory avatar alex-cory commented on May 15, 2024

You can do that, however it's always recommended that you don't have api calls/awaits in loops. Why do you need to make an api call for image urls? If you're displaying images, why not just do imageUrls.map(url => <img src={url} />)

from use-http.

supermihi avatar supermihi commented on May 15, 2024

In my application I am fetching data, not images – that was just for the example.

from use-http.

alex-cory avatar alex-cory commented on May 15, 2024

You could do something like

const Comp = (props) => {
  const [imagesData, setImagesData] = useState([])
  const [loading, setLoading] = useState(false)
  const { get } = useFetch('url')

  const getImageData = useCallback(async () => {
    setLoading(true)
    const imgData = await images.map(async image =>  await get('/image?id=${image.id}'))
    setImagesData(imgData)
    setLoading(false)
  }, [props.images, imagesData, setImagesData, setLoading])

  const mounted = useRef(false)
  useEffect(() => {
    if (!mounted.current) { // on mount
      getImagesData()
    }
  }, [getImagesData])

  return (
    <>
      {loading && 'Loading...'}
      {imagesData.length > 0 && imagesData.map(img => <img src={img.url} />)}
    </>
  )
}

Is this something like what you're looking for?

from use-http.

alex-cory avatar alex-cory commented on May 15, 2024

@supermihi is your question answered? Gonna close this if it is :)

from use-http.

supermihi avatar supermihi commented on May 15, 2024

@alex-cory thanks for your answer. The above should work (though I'd remove the mounted construct, which would prevent re-loading if props.images changes, right?).
However I don't really see the benefit of useFetch here, as opposed to calling fetch directly?

from use-http.

alex-cory avatar alex-cory commented on May 15, 2024

Oh yeah, that makes sense about the mounted part. In this case, the only benefit would be if you used the <Provider url='https://example.com'><App /></Provider>. Kind of like how axios works. Also, if you have authentication, you could handle that at the top level in addition to the syntax is a bit cleaner. Having the global fetch config is pretty nice.

from use-http.

supermihi avatar supermihi commented on May 15, 2024

Ok, thanks a lot!

from use-http.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.