1.0.22 • Published 10 months ago

@joshdoesthis/react-router v1.0.22

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

React Router

A simple router for React apps.

Installation

Using npm:

$ npm i @joshdoesthis/react-router

Using yarn:

$ yarn add @joshdoesthis/react-router

Usage

Basic Routes

import { Router, Route, Link, useRouter } from '@joshdoesthis/react-router'

const Home = () => {
  const router = useRouter()

  return (
    <div>
      <h1>Home</h1>
      <Link path='/page/1'>Page 1</Link>
      <Link path='/page/2'>Page 2</Link>
      <button onClick={() => router.navigate('/page/3')}>Page 3</button>
    </div>
  )
}

const Page = () => {
  const router = useRouter()
  const { params } = router.state

  return <h1>Page: {params.pageId}</h1>
}

const NotFound = () => {
  const router = useRouter()

  return (
    <div>
      <h1>Not Found</h1>
      <button onClick={() => router.back()}>Go Back</button>
    </div>
  )
}

const App = () => {
  return (
    <Router>
      <Route path='/' component={Home} />
      <Route path='/page/:pageId' component={Page} />
      <Route path='/404' component={NotFound} />
    </Router>
  )
}

Authenticated Routes

import { Router, Route, Link, useRouter } from '@joshdoesthis/react-router'
import { Auth, useAuth } from '../provider/auth'

const Home = () => {
  const router = useRouter()

  return (
    <div>
      <h1>Home</h1>
      <Link path='/page/1'>Page 1</Link>
      <Link path='/page/2'>Page 2</Link>
      <button onClick={() => router.navigate('/page/3')}>Page 3</button>
    </div>
  )
}

const Page = () => {
  const router = useRouter()
  const { params } = router.state

  return <h1>Page: {params.pageId}</h1>
}

const Login = () => {
  return <h1>Login</h1>
}

const NotFound = () => {
  const router = useRouter()

  return (
    <div>
      <h1>Not Found</h1>
      <button onClick={() => router.back()}>Go Back</button>
    </div>
  )
}

const App = () => {
  const auth = useAuth()

  return (
    <Router authenticated={auth.state.authenticated}>
      <Route path='/' component={Home} />
      <Route auth path='/page/:pageId' component={Page} />
      <Route path='/login' component={Login} />
      <Route path='/404' component={NotFound} />
    </Router>
  )
}

const AuthenticatedApp = () => {
  return (
    <Auth>
      <App />
    </Auth>
  )
}

Router

PropTypeDescription
authenticatedbooleanWhether or not the user is authenticated. Defaults to false.
authRedirectstringThe path to redirect to if the user is not authenticated. Defaults to /login.
notFoundRedirectstringThe path to redirect to if the route is not found. Defaults to /404.

Route

PropTypeDescription
authbooleanWhether or not the route requires authentication. Defaults to false.
notFoundbooleanWhether or not the route is the not found route. Defaults to false.
pathstringThe path to match.
componentfunctionThe component to render.

Link

PropTypeDescription
pathstringThe path to link to.
extbooleanWhether or not the link is external. Defaults to false.
targetstringThe target of the link. Defaults to _self.
styleobjectThe style of the link.

useRouter

PropTypeDescription
stateobjectThe current state of the router.
navigate(path: string) => voidNavigates to the specified path.
back() => voidNavigates back to the previous path.
1.0.19

10 months ago

1.0.18

10 months ago

1.0.17

10 months ago

1.0.16

10 months ago

1.0.22

10 months ago

1.0.21

10 months ago

1.0.20

10 months ago

1.0.11

11 months ago

1.0.15

10 months ago

1.0.14

10 months ago

1.0.13

10 months ago

1.0.12

10 months ago

1.0.9

11 months ago

1.0.8

11 months ago

1.0.7

11 months ago

1.0.10

11 months ago

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago