v3.0.0
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 asAxios
. Thefetcher
function must return an object with at least thedata
andstatus
properties (you can also return ajson
method instead ofdata
). - The object created with
Client.create
now also includes theconfig
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>
)
}