Coder Social home page Coder Social logo

upstatement / react-router-guards Goto Github PK

View Code? Open in Web Editor NEW
129.0 17.0 19.0 5.88 MB

Guard middleware for React Router navigation

Home Page: https://www.npmjs.com/package/react-router-guards

JavaScript 6.38% TypeScript 93.62%
reactjs hooks rollup typescript

react-router-guards's Introduction

Logo

react-router-guards

Guard middleware for React Router navigation

React Router Guards provides a middleware API for React Router, allowing you to perform complex logic between the call for navigation and the final render of a route.

Table of Contents

Requirements

This package has the following peer dependencies:

Installation

With npm:

$ npm install react-router-guards

With yarn:

$ yarn add react-router-guards

Then with a module bundler like webpack, use as you would anything else:

// using ES6 modules
import { GuardProvider, GuardedRoute } from 'react-router-guards';

// using CommonJS modules
const GuardProvider = require('react-router-guards').GuardProvider;
const GuardedRoute = require('react-router-guards').GuardedRoute;

Basic usage

Here is a very basic example of how to use React Router Guards.

import React from 'react';
import { BrowserRouter } from 'react-router-dom';
import { GuardProvider, GuardedRoute } from 'react-router-guards';
import { About, Home, Loading, Login, NotFound } from 'pages';
import { getIsLoggedIn } from 'utils';

const requireLogin = (to, from, next) => {
  if (to.meta.auth) {
    if (getIsLoggedIn()) {
      next();
    }
    next.redirect('/login');
  } else {
    next();
  }
};

const App = () => (
  <BrowserRouter>
    <GuardProvider guards={[requireLogin]} loading={Loading} error={NotFound}>
      <Switch>
        <GuardedRoute path="/login" exact component={Login} />
        <GuardedRoute path="/" exact component={Home} meta={{ auth: true }} />
        <GuardedRoute path="/about" exact component={About} meta={{ auth: true }} />
        <GuardedRoute path="*" component={NotFound} />
      </Switch>
    </GuardProvider>
  </BrowserRouter>
);

Check out our demos for more examples!

Concepts

With the addition of guard middleware, the navigation lifecycle has changed.

Guard functions are the middleware between navigation and rendering.

Page components are used for setting loading and error pages.

The GuardProvider component is a high-level wrapper for your entire routing solution.

The GuardedRoute component acts as a replacement for the default Route component provided by React Router, allowing for routes to use guard middleware.

Demos

We've included some demos below to help provide more context on how to use this package!

Basic

Demo + Source

The basic demo showcases some basic functionality of route guard API with an auth example.

Intermediate

Demo | Source

The intermediate demo uses the PokéAPI to showcase how to use route guards for fetching data from an API.

Contributing

We welcome all contributions to our projects! Filing bugs, feature requests, code changes, docs changes, or anything else you'd like to contribute are all more than welcome! More information about contributing can be found in the contributing guidelines.

Code of Conduct

Upstatement strives to provide a welcoming, inclusive environment for all users. To hold ourselves accountable to that mission, we have a strictly-enforced code of conduct.

About Upstatement

Upstatement is a digital transformation studio headquartered in Boston, MA that imagines and builds exceptional digital experiences. Make sure to check out our services, work, and open positions!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.