use-prefetch
v0.1.1
Published
Hook and component for prefetch
Downloads
9
Readme
use-prefetch
Hook and component for prefetch.
Installation
npm i use-prefetch
Usage
usePrefetch
This hook returns a callback ref.
DOM nodes that are passed the callback ref, handle following events:
mouseover
,pointerover
- Schedule thatprefetch
will be called after specified delay time.mouseout
,pointerout
- Cancel the scheduledprefetch
.mousedown
,pointerdown
,touchstart
- Callprefetch
immediately.
To avoid calling prefetch
in succession, you can specify a cooldown time.
import { usePrefetch } from 'use-prefetch';
function Component() {
const callbackRef = usePrefetch(() => {
fetchResource();
}, {
cooldown: 300000,
delay: 200,
});
return (
<a href='...' ref={callbackRef}>resource</a>
);
}
Prefetch
Prefetch
is component version of usePrefetch
.
import { Prefetch } from 'use-prefetch';
function Component() {
return (
<Prefetch
prefetch={() => fetchResource()}
cooldown={300000}
delay={200}
render={callbackRef =>
<a href='...' ref={callbackRef}>resource</a>
}
/>
);
}
License
MIT