All Projects → sindresorhus → React Router Util

sindresorhus / React Router Util

Licence: mit
Useful components and utilities for working with React Router

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Router Util

react-windows-ui
Build Windows fluent UI apps using ReactJS. Provides a set of accessible, reusable, and composable React components that make it super easy to create websites and apps.
Stars: ✭ 383 (+19.69%)
Mutual labels:  react-component, npm-package
react-daterange-picker
A react date range picker to using @material-ui. Live Demo: https://flippingbitss.github.io/react-daterange-picker/
Stars: ✭ 85 (-73.44%)
Mutual labels:  react-component, npm-package
use-react-router-breadcrumbs
tiny, flexible, hook for rendering route breadcrumbs with react-router v6
Stars: ✭ 170 (-46.87%)
Mutual labels:  router, react-router
Registration-and-Login-using-MERN-stack
Simple Registration and Login component with MERN stack
Stars: ✭ 210 (-34.37%)
Mutual labels:  react-router, react-component
yarr
A React router library enabling the render-as-you-fetch concurrent UI pattern.
Stars: ✭ 97 (-69.69%)
Mutual labels:  router, react-router
git-issue-react-electronjs
⚙️. ⚛️. A desktop application created with Electronjs and Reactjs to be cross-platform to manage and track GitHub issues.
Stars: ✭ 21 (-93.44%)
Mutual labels:  react-router, react-component
smoothr
A custom React router that leverages the Web Animations API and CSS animations.
Stars: ✭ 28 (-91.25%)
Mutual labels:  router, react-router
React Live Route
📌 An enhanced react-router-v4/5 Route that keeps route alive.
Stars: ✭ 207 (-35.31%)
Mutual labels:  router, react-router
fritz-box
📦 Promise-based JavaScript FRITZ!Box API.
Stars: ✭ 14 (-95.62%)
Mutual labels:  router, npm-package
cra-redux-boilerplate
⚛️🔨create-react-app application with redux and another cool libraries to make your life easier.
Stars: ✭ 15 (-95.31%)
Mutual labels:  router, react-router
whatsapp-clone-react
Build a WhatsApp Clone with React JS and FireBase.
Stars: ✭ 38 (-88.12%)
Mutual labels:  react-router, react-component
react-pits
React 中的坑
Stars: ✭ 29 (-90.94%)
Mutual labels:  react-router, react-component
awesome-web-react
🚀 Awesome Web Based React 🚀 Develop online with React!
Stars: ✭ 31 (-90.31%)
Mutual labels:  react-router, react-component
react-circle-flags
🚀 A React component with a collection of 300+ minimal circular SVG country flags. Wrapper of HatScripts/circle-flags
Stars: ✭ 29 (-90.94%)
Mutual labels:  react-component, npm-package
Type Fest
A collection of essential TypeScript types
Stars: ✭ 6,623 (+1969.69%)
Mutual labels:  utilities, npm-package
boring-router
A type-safe MobX router with parallel routing support.
Stars: ✭ 74 (-76.87%)
Mutual labels:  router, react-router
Redux First History
🎉 Redux First History - Redux history binding support react-router - @reach/router - wouter
Stars: ✭ 163 (-49.06%)
Mutual labels:  router, react-router
React Router Native Stack
A stack navigation component for react-router-native
Stars: ✭ 171 (-46.56%)
Mutual labels:  router, react-router
react-native-value-picker
Cross-Platform iOS(ish) style picker for react native.
Stars: ✭ 18 (-94.37%)
Mutual labels:  react-component, npm-package
froute
Type safe and flexible router for React
Stars: ✭ 31 (-90.31%)
Mutual labels:  router, react-router

react-router-util

Useful components and utilities for working with React Router

Install

$ npm install react-router-util

Usage

import {ipcRenderer as ipc} from 'electron';
import React from 'react';
import {Route, Link} from 'react-router-dom';
import {history, BrowserRouter as Router, Debug} from 'react-router-util';

ipc.on('goto-about', () => {
	history.push('/about');
});

const App = () => (
	<Router>
		<>
			<Debug/>

			<ul>
				<li><Link to="/">Home</Link></li>
				<li><Link to="/about">About</Link></li>
			</ul>

			<hr/>

			<Route exact path="/" component={Home}/>
			<Route path="/about" component={About}/>
		</>
	</Router>
);

API

history

A history singleton that you can use in <Router history={history}> to access the history from outside the router. Can be useful for programmatically navigating to a route when used in combination with non-React code, like Electron IPC events, etc.

<BrowserRouter> and <StaticRouter>

Same as the official <BrowserRouter> and <StaticRouter>, but with history={history} set to the above history singleton, so you can just import the singleton to access the router history object from outside the router. This behavior can be overridden with <BrowserRouter history={yourOwnHistory}>.

<Debug/>

Unrendered React component that prints the props of the current route component to the console when not in production.

<CurrentRoute/>

React component that renders the pathname of the current route. For example: /dashboard.

<RouteWithProps/>

Like <Route/>, but passes additional props to the given component. The following props are passed to the route: path, component, exact, strict, location, sensitive, while the rest are passed to the component.

Before:

<Route path="/unicorn" render={props => <Unicorn {...props} foo={'cake'} bar/>}/>

After:

<Route path="/unicorn" component={Unicorn} foo={'cake'} bar/>

<AuthenticatedRoute/>

An authenticated version of <Route/>. You pass it an isAuthenticated prop with a boolean of whether it's authenticated. If it's true, it will render the given component or redirect to the given redirectTo path. If it's false, it will redirect to /login or loginPath if specified. You can specify redirectFromLoginTo to have it redirect somewhere from the loginPath when authenticated. It accepts all the props <Route/> accepts except for render. Additional props are passed to the component. You can also pass it children.

Before:

<Route path="/" exact render={props => (
	isLoggedIn ? <MainView {...props}/> : <Redirect to="/login"/>
)}/>

After:

<AuthenticatedRoute
	path="/"
	exact
	isAuthenticated={isLoggedIn}
	component={MainView}
/>

Another example:

<AuthenticatedRoute
	path="/admin"
	isAuthenticated={isLoggedIn}
	redirectTo="/admin/dashboard"
	loginPath="/admin/login"
/>

Yet another example:

<AuthenticatedRoute isAuthenticated={this.state.isLoggedIn} redirectFromLoginTo="/dashboard">
	<Switch>
		<RouteWithProps path="/login" component={Login} {...this.state}/>
		<RouteWithProps component={Main} {...this.state}/>
	</Switch>
</AuthenticatedRoute>

Example with nested routes:

<AuthenticatedRoute path="/dashboard/:nested" isAuthenticated={this.state.isLoggedIn}>
	<Switch>
		<RouteWithProps path="/information" component={Information} {...this.state}/>
		<RouteWithProps path="/contact" component={Contact} {...this.state}/>
	</Switch>
</AuthenticatedRoute>

When using nested routes, the :nested value must be specified in the outer route so that matchPath can map it to the correct nested route (/information and /contact in this case).

<ConditionalRoute/>

A conditional version of <Route/>. You pass it a conditional prop with a boolean. If it's truthy, either the given trueComponent will be rendered or it will redirect to trueRedirectTo. If it's falsy, either the given falseComponent will be rendered or it will redirect to trueRedirectTo. It accepts all the props <Route/> accepts except for render.

Before:

<Route path="/" exact render={() => (
	isLoggedIn ? <MainView/> : <Redirect to="/login"/>
)}/>

After:

<ConditionalRoute
	path="/"
	exact
	conditional={isLoggedIn}
	trueComponent={MainView}
	falseRedirectTo="/login"
/>

It's a little bit more verbose, but declarative FTW.

<BackLink>

Like <Link>, but navigates to the previous route in the history. Accepts any props <Link> supports except for to.

<ForwardLink>

Like <Link>, but navigates to the next route in the history. Accepts any props <Link> supports except for to.

Related

  • react-extras - Useful components and utilities for working with React
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].