Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including react-preload-link with all npm packages installed. Try it out:

require("react/package.json"); // react is a peer dependency. require("react-dom/package.json"); // react-dom is a peer dependency. require("react-router/package.json"); // react-router is a peer dependency. require("react-router-dom/package.json"); // react-router-dom is a peer dependency. var reactPreloadLink = require("react-preload-link")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

react-preload-link v1.0.0

A superversion of React Router's Link that preloads your data before navigating. Built with and for React.


npm version npm downloads

A superversion of React Router's Link component that preloads your data before navigating. Built with and for React.


Live demo:



npm install --save react-preload-link


yarn add react-preload-link


import PreloadLink from 'react-preload-link';

const PreloadExample = () => {
    const loadProfile = () => {
        return fetch('')
            .then(result => doSomething(result));
    const loadFriends = () => {
        return fetch('')
            .then(result => doSomething(result));
    return (
            <PreloadLink to="profile" load={loadProfile}>
                View profile
            <PreloadLink to="friends" load={[loadProfile, loadFriends]}>
                View friends


PreloadLinkProps {
    to: string,
    load?: () => Promise<any> | Array<() => Promise<any>>,
    onLoading?: (defaultHook: () => void) => any,
    onSuccess?: (defaultHook: () => void) => any,
    onFail?: (defaultHook: () => void) => any,
    onNavigate?: (defaultHook: () => void) => any,
    loadMiddleware?: (data: any) => any,
    noInterrupt?: boolean,
    className?: string,
    navLink?: boolean,
    activeClassName?: string,
    onClick?: () => any,

to (Required)


The URL to which the app will navigate to. If load is used, it will navigate after the load function(s) resolve. The only required prop.


() => Promise<any> | Array<() => Promise<any>>

Promise that will be resolved before navigating to the URL provided by to. This can be a single function or multiple in an Array. Must be a function that returns a Promise! It will wait for everything to resolve before navigating.

Note: If you see an error that says "Can not read then of undefined" then one of your passed functions does not return a Promise.

onLoading, onSuccess, onFail, onNavigate

(defaultHook: () => void) => void

Overriding the default hooks, set with configure, can be done with these props. The default hook is passed as a parameter.


(data: any) => void

Will fire for each of your resolved Promises. The resolved data is passed as a parameter.

Note: This will fire multiple times if you pass an Array to load.


boolean | Default false

PreloadLinks with this prop can not have their load be interrupted by any other PreloadLink.



The CSS class to be set on the <a> element.


boolean | Default false

Use React-Router's NavLink instead of Link. This will give it the same benefits as the regular NavLink.



CSS class to be set on the <a> element when its route is active.


() => void

For any methods that should be fired instantly on click. Use this for methods that are not async.

Note: will not fire if another PreloadLink process is busy and this process is not interruptible. This happens when a noInterrupt PreloadLink was clicked.

Configuring lifecycle hooks

You can create hooks for the various lifecycle methods of PreloadLink. These functions will only be called if a load prop is passed to the component. These functions will only be called once, even if you pass an array to load. It's recommended to only use configure once, like at the entry of your app.


You can hook up a global loader with these hooks very easily. Here is an example on how to use NProgress together with PreloadLink.

import { configure } from 'react-preload-link';
import NProgress from 'nprogress';

    onLoading: NProgress.start,
    onFail: NProgress.done,
    onSuccess: NProgress.done,


PreloadLinkConfigOptions {
    onLoading?: () => any;
    onSuccess?: () => any;
    onFail?: () => any;
    onNavigate?: () => any;


() => any

Fires once when the load process has started. Only fires when load prop has been used.


() => any

Fires once when the all Promises have been resolved. Success is fired before navigation. Only fires when load prop has been used.


() => any

Fires once when one of the Promised from load fails or rejects. Only fires when load prop has been used.


() => any

Fires once after navigation. Always fires.


RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free