npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-preload-link

v1.0.0

Published

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

Downloads

19

Readme

react-preload-link

npm version npm downloads

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

Demo

Live demo: https://sandervspl.github.io/react-preload-link/

Installation

NPM

npm install --save react-preload-link

YARN

yarn add react-preload-link

Usage

import PreloadLink from 'react-preload-link';

const PreloadExample = () => {
    const loadProfile = () => {
        return fetch('https://someapi.com/api/v1/user/me')
            .then(result => doSomething(result));
    };
    
    const loadFriends = () => {
        return fetch('https://someapi.com/api/v1/user/1/friends')
            .then(result => doSomething(result));
    }
    
    return (
        <div>
            <PreloadLink to="profile" load={loadProfile}>
                View profile
            </PreloadLink>
            
            <PreloadLink to="friends" load={[loadProfile, loadFriends]}>
                View friends
            </PreloadLink>
        </div>
    );
};

Props

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)

string

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.

load

() => 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.

loadMiddleware

(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.

noInterrupt

boolean | Default false

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

className

string

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

navLink

boolean | Default false

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

activeClassName

string

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

onClick

() => 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.

Example

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';

configure({
    onLoading: NProgress.start,
    onFail: NProgress.done,
    onSuccess: NProgress.done,
)};

Type

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

onLoading

() => any

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

onSuccess

() => any

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

onFail

() => any

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

onNavigate

() => any

Fires once after navigation. Always fires.