Skip to content

Anyway to use the same useFetch for multiple calls? #399

@TannerS

Description

@TannerS

Any way to use the use the same use fetch and be able to tell apart call's responses? Here is an example, for the most part it looks like it would work, but how could the methods loadData1 and loadData2 be able to tell if response.ok is from which api call?

import useFetch from 'use-http'

function Todos() {
  const [path1Data, setPath1Data] = useState([])
  const [path2Data, setPathData] = useState([])

  const { get, response } = useFetch('')

  const loadData1 = async () => {
    const data1 = await get('/path1')
    if (response.ok) setPath1Data(data1)
  }


  const loadData2 = async () => {
    const data2 = await get('/path2')
    if (response.ok) setPath2Data(data2)
  }

  // componentDidMount
  useEffect(() => {
    loadData1()
    loadData2()
  }, [])

Thank you

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions