1.0.15 • Published 4 years ago

react-router-reconfig v1.0.15

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

React-router-reconfig

Installation

To install, you can use npm:

$ npm install react-router-reconfig

Motivation

The library makes it easy to write routing in the application. Makes its structure obvious, as well as it can be divided into child configs and combined into one. Supports nested routes (renderNestedRoute) and typescript. The library has a built-in guard system. When you click on child links, the parent will not be updated.

Demo

https://codesandbox.io/s/guards-x9xky

Usage

import React from 'react'
import ReactDOM from 'react-dom'
import { BrowserRouter, Link, Switch, Redirect } from 'react-router-dom'
import {
  createRoutes,
  RenderNestedRoute,
  RouteTypes
} from 'react-router-reconfig'

const useUser = () => ({
  user: {
    id: '',
    rules: []
  }
})

const FirstPage = () => {
  return <div>first page</div>
}

type Props = {
  renderNestedRoute: RenderNestedRoute
}

const SecondPage = ({ renderNestedRoute }: Props) => {
  return (
    <div>
      <h1>second page</h1>
      {renderNestedRoute({ someProps: 'someProps' })}
    </div>
  )
}

const SecondPageChild1 = () => {
  return <div>second page SecondPageChild1</div>
}

const SecondPageChild2 = () => {
  return <div>second page SecondPageChild2</div>
}

const routes = (): RouteTypes => [
  {
    component: FirstPage,
    path: '/',
    guards: [onlyAuth]
  },
  {
    component: SecondPage,
    exact: false,
    path: '/second',
    guards: [onlyAuth],
    children: [
      {
        component: SecondPageChild1,
        path: '/child-first',
        guards: [onlyAuth, onlyAdmin]
      },
      {
        component: SecondPageChild2,
        path: '/child-second',
        guards: [onlyAuth, onlyRoles([onlyAdmin, onlyManager])],
        fallback: () => <Redirect to="/" />
      },
      {
        component: () => <h1>page not found</h1>,
        path: '*'
      }
    ]
  },
  {
    component: () => <h1>page not found</h1>,
    path: '*'
  }
]

type RouteProps = {
  user: {
    id: number
  }
}

function App() {
  const { user } = useUser()
  const Routes = React.useMemo(
    () =>
      createRoutes <
      RouteProps >
      {
        config: routes(),
        context: { user }
      },
    [user]
  )

  return (
    <>
      <BrowserRouter>
        <Switch>{Routes}</Switch>
      </BrowserRouter>
    </>
  )
}

const rootElement = document.getElementById('root')
ReactDOM.render(<App />, rootElement)

Route-config

nameTypedescriptionrequiredlinks
componentReactNodemodal open functionfalse
exactbooleanmodal close requesttrue
pathstringopened modalstrue
childrenArray<{}>id active modalfalse
guards(context) => booleanfalsefalseexample
fallbackReactNodefalsefalse

createRoutes types

nameTypedescriptionrequiredlinks
configarrayrouter-configtrueexample
contextobjectusertrue
genericcustomcreateRoutesfalse

renderNestedRoute types

nameTypedescriptionrequired
propsobjectyou propsfalse

Example-guards

function onlyAuth(context) {
  return Boolean(context.user)
}

function onlyAdmin(context) {
  const user = context.user
  if (user) {
    return user.rules.some(rule => rule.name === 'Administrator')
  }
}

function onlyManager(context) {
  const user = context.user
  if (user) {
    return user.rules.some(rule => rule.name === 'Manager')
  }
}

function onlyRoles(roles) {
  return context => {
    return roles.some(role => role(context))
  }
}

//.... and other you function
1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago