{"library":"react-router-guards","title":"react-router-guards","description":"Provides a middleware API for React Router (v5), allowing developers to run guard functions between navigation and route rendering. Currently at version 1.0.2, it is stable but not actively maintained (last update 2019). Key features include GuardProvider and GuardedRoute components, support for meta data on routes, loading/error components, and TypeScript types. Unlike simple auth wrappers, it offers a full lifecycle with next() callbacks for redirects, async guards, and error handling.","language":"javascript","status":"maintenance","last_verified":"Sat Apr 25","install":{"commands":["npm install react-router-guards"],"cli":null},"imports":["import { GuardProvider } from 'react-router-guards'","import { GuardedRoute } from 'react-router-guards'","const requireLogin = (to, from, next) => { next(); }"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import React from 'react';\nimport { BrowserRouter, Switch } from 'react-router-dom';\nimport { GuardProvider, GuardedRoute } from 'react-router-guards';\n\nconst Home = () => <div>Home</div>;\nconst Login = () => <div>Login</div>;\nconst Loading = () => <div>Loading...</div>;\nconst Error = () => <div>Error</div>;\n\nconst requireAuth = (to, from, next) => {\n  const isLoggedIn = localStorage.getItem('token');\n  if (to.meta.auth && !isLoggedIn) {\n    next.redirect('/login');\n  } else {\n    next();\n  }\n};\n\nfunction App() {\n  return (\n    <BrowserRouter>\n      <GuardProvider guards={[requireAuth]} loading={Loading} error={Error}>\n        <Switch>\n          <GuardedRoute exact path=\"/login\" component={Login} />\n          <GuardedRoute exact path=\"/\" component={Home} meta={{ auth: true }} />\n        </Switch>\n      </GuardProvider>\n    </BrowserRouter>\n  );\n}\n\nexport default App;","lang":"typescript","description":"Sets up guard middleware with a loading component, an auth guard that redirects unauthenticated users, and meta data on routes.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}