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

@jayarjo/react-router-loading

v0.5.5

Published

Custom react router switch that allows you to load data before switching the screen

Downloads

11

Readme

react-router-loading

Custom react router switch that allows you to load data before switching the screen

npm version

The latest version is React Router 6 friendly, which basically means that it won't work with React Router 5. If you need it to work with React Router 5, use version ^0.4.x.


DEMO

Requirements

"react": "^16.8.0 || ^17.0.0",
"react-dom": "^16.8.0 || ^17.0.0",
"react-router-dom": "^5.0.0"

This package uses react-router-dom as main router so you should implement it in your project first.

Installation

npm install react-router-loading
## or
yarn add react-router-loading

Usage

In your router section import Routes and Route from react-router-loading instead of react-router-dom

import { Routes, Route } from 'react-router-loading';

<Routes>
    <Route path="/page1" component={Page1} />
    <Route path="/page2" component={Page2} />
    ...
</Routes>;

Add loading prop to every route that must be loaded before switching

<Routes>
    // data will be loaded before switching
    <Route path="/page1" component={Page1} loading />
    // instant switch as before
    <Route path="/page2" component={Page2} />
    ...
</Routes>

Add loadingContext.done() at the end of your initial loading method in components that mentioned in routes with loading prop (in this case it's Page1)

import { LoadingContext } from 'react-router-loading';
const loadingContext = useContext(LoadingContext);

const loading = async () => {
    // loading some data

    // call method to indicate that loading is done and we are ready to switch
    loadingContext.done();
};

or for class components

import { LoadingContext } from "react-router-loading";

class ClassComponent extends React.Component {
    ...
    loading = async () => {
        // loading some data

        // call method from props to indicate that loading is done
        this.props.loadingContext.done();
    };
    ...
};

// we should wrap class component with Context Provider to get access to loading methods
const ClassComponentWrapper = (props) =>
    <LoadingContext.Consumer>
        {loadingContext => <ClassComponent loadingContext={loadingContext} {...props} />}
    </LoadingContext.Consumer>

Config

You can specify loading screen that would be shown at the first loading of your app

const MyLoadingScreen = () => <div>Loading...</div>

<Routes loadingScreen={MyLoadingScreen}>
...
</Routes>

Use maxLoadingTime property if you want to limit loading time. Pages will switch if loading takes more time than specified in this property (ms).

<Routes maxLoadingTime={500}>...</Routes>

If you want to change LoadingContext globally you can pass isLoading property to the Routes. This way you don't need to add extra loadingContext.done(); in your page components after fetching is done.

import { useIsFetching } from 'react-query';
const isFetching = useIsFetching();

<Routes isLoading={isFetching}>...</Routes>;

Call topbar.config() if you want to change topbar configuration. More info here.

import { topbar } from 'react-router-loading';

topbar.config({
    autoRun: false,
    barThickness: 5,
    barColors: {
        0: 'rgba(26,  188, 156, .7)',
        0.3: 'rgba(41,  128, 185, .7)',
        1.0: 'rgba(231, 76,  60,  .7)',
    },
    shadowBlur: 5,
    shadowColor: 'red',
    className: 'topbar',
});

How to run example on localhost

Clone repository and run

yarn build && yarn start

License

MIT