Skip to content

v3.0.0

Compare
Choose a tag to compare
@danybeltran danybeltran released this 03 Feb 01:53
· 131 commits to master since this release
a61c35c

Feat: fetcher

  • The fetching mechanism is now configurable by passing the fetcher prop. This can be set globally using the <FetchConfig> context component or per-request. This opens the posibility of using other data-fetching libraries such as Axios. The fetcher function must return an object with at least the data and status properties (you can also return a json method instead of data).
  • The object created with Client.create now also includes the config property, which can be passed to the <FetchConfig> component.

Example of a custom fetcher with axios:

import useFetch from 'http-react'
import axios from 'axios'

const fetcher = (url, config) => axios(url, config)

export default function Profile() {
  const { data, loading, error, code, id } = useFetch('/api/profile', {
    fetcher
  })

  if (loading) return <p>Loading...</p>

  if (error) return <p>Failed to fetch</p>

  return (
    <div>
      <h2>Profile</h2>
      <p>Name: {data.name}</p>
    </div>
  )
}