prhttp-react
v1.0.0
Published
React lifecycle binding for prhttp
Downloads
3
Readme
prhttp-react
React Component lifecycle binding for prhttp.
Installation
npm install prhttp-react
Usage
Class Component
Use HOC to bind Class Component.
import { bindLifecycle } from 'prhttp-react'
// PrHttpClient instance
const httpClient = /.../
const withClient = bindLifecycle(httpClient)
const BoundComponent = withClient(WrappedComponent)
Function Component
Use Effect Hook to bind Function Component.
import { useLifecycle } from 'prhttp-react'
import { CancelError } from 'prhttp'
function useRequest() {
useEffect(() => {
getExample().then( () => {
// Handle response
}).catch( e => {
if(e instanceof CancelError) {
console.log(`Request was cancelled`)
} else {
console.log(e)
}
})
})
}
function Home() {
// Call useLifecycle before request
useLifecycle(httpClient)
useRequest()
return (
<div>
<h2>Home</h2>
</div>
);
}