
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
react-router-guards
Advanced tools
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.
This package has the following peer dependencies:
React v16.8.0+ (for hooks ⚓️)
React Router DOM v5.0.0+
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;
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!
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
GuardProvidercomponent is a high-level wrapper for your entire routing solution.
The
GuardedRoutecomponent acts as a replacement for the defaultRoutecomponent provided by React Router, allowing for routes to use guard middleware.
We've included some demos below to help provide more context on how to use this package!
The basic demo showcases some basic functionality of route guard API with an auth example.
The intermediate demo uses the PokéAPI to showcase how to use route guards for fetching data from an API.
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.
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.
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!
FAQs
Guard middleware for React Router navigation
The npm package react-router-guards receives a total of 1,762 weekly downloads. As such, react-router-guards popularity was classified as popular.
We found that react-router-guards demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.