1.2.0 • Published 3 years ago
react-fetch-it-hook v1.2.0
#REACT FETCH IT HOOK
yarn add react-fetch-it-hook
##getting started
add provider
ReactDOM.render(
<React.StrictMode>
<FetchProvider value={{basePath: 'http://demo7542668.mockable.io', logLevel: 'INFO'}}>
<App/>
</FetchProvider>
</React.StrictMode>,
document.getElementById('root')
)
fetch example
interface IBaseResponse {
body: unknown
}
interface IHelloResponse extends IBaseResponse {
body: {
messagge: string
}
}
interface IHelloRequest {
user: string
}
export const DumpFetchComponent: React.FC = props => {
// can rename the response with comma
const {response: helloResponse, error, loading, call} = useFetch<IHelloResponse, IHelloRequest>({
url: '/{test}',
method: HttpMethod.POST
})
useEffect(() => {
setTimeout(() => {
call({user: '1'}, {test: 'hello'}, {last: 10})
}, 1000)
}, [])
useEffect(() => {
console.info('final error ', error)
}, [error])
useEffect(() => {
console.info('final response', helloResponse)
}, [helloResponse])
useEffect(() => {
console.info('loading ', loading ? 'yes' : 'no')
}, [loading])
return <div> RESPONSE {helloResponse && JSON.stringify(helloResponse)}</div>
}
issues
- works just debug log