5.0.3 • Published 4 years ago

router-hook v5.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago
// index.js
import { RouterProvider } from "router-hook"


ReactDOM.render(
  <RouterProvider>
    <App />
  </RouterProvider>
  document.getElementById("root")
)
// routes.js
import { route } from "router-hook"

export const homeRoute = route("/")
export const registerRoute = route("/register")
export const loginRoute = route("/login")
export const accountRoute = route("/account")
export const editRoute = route("/thing/edit/:id?")
// Router.js
import { useLocation, useSetRoute, A } from "router-hook"
import {
  homeRoute,
  registerRoute,
  loginRoute,
  accountRoute,
  editRoute
} from "./routes"

function Router() {
  const location = useLocation()
  const setRoute = useSetRoute()
  const [user] = useUser()
  const path = location.pathname

  const auth = render => {
    if (user && user.id) {
      return render
    }
    return () => {
      setRoute(loginRoute.toUrl())
      return null
    }
  }

  return (
    <>
      <A className="App-link" href={loginRoute.toUrl()}>
        Login
      </A>
      {homeRoute.match(path, () => (
        <Home />
      ))}
      {registerRoute.match(path, () => (
        <Register />
      ))}
      {loginRoute.match(path, () => (
        <Login />
      ))}
      {accountRoute.match(
        path,
        auth(() => <Account />)
      )}
      {editRoute.match(
        path,
        auth(({ id }) => <Edit id={id} />)
      )}
    </>
  )
}
5.0.3

4 years ago

5.0.2

4 years ago

5.0.1

5 years ago

5.0.0

5 years ago

4.0.4

5 years ago

4.0.1

5 years ago

4.0.0

5 years ago

4.0.3

5 years ago

4.0.2

5 years ago

3.0.6

5 years ago

3.0.5

5 years ago

3.0.4

5 years ago

3.0.3

5 years ago

3.0.2

5 years ago

3.0.1

5 years ago

3.0.0

6 years ago

2.3.0

6 years ago

2.2.1

6 years ago

2.2.0

6 years ago

2.1.0

6 years ago

2.0.0

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago