Giter Site home page Giter Site logo

just1and0 / react-tiniest-router Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kitze/react-tiniest-router

0.0 1.0 0.0 146 KB

The tiniest router for React, built with hooks

Home Page: https://codesandbox.io/s/react-tiniest-router-demo-pejt4

HTML 5.07% TypeScript 94.93%

react-tiniest-router's Introduction

react-tiniest-router

For the times when you really need a simple router.
Based on mobx-router and rttr.

Usage

  1. Write the routes object.
const routes = {
  home: {
    id: 'home',
    path: '/',
  },
  about: {
    id: 'about',
    path: '/about',
  },
  gallery: {
    id: 'gallery',
    path: '/gallery/:imageId',
  },
}
  1. Wrap your app with the Router component
<Router routes={routes}>
    <App />
</Router>
  1. Use the router using useRouter
  • Use the goTo function for navigating to a route
  • Use the isRoute function for checking if a route is currently active
const Root = () => {
  const {goTo, isRoute} = useRouter();

  return (
    <div>
      <div>
        <button onClick={() => goTo(routes.home)}>go home</button>
        <button onClick={() => goTo(routes.about)}>go to about</button>
        <button onClick={() => goTo(routes.gallery, { imageId: 1 })}>
          go to picture 1
        </button>
        <button onClick={() => goTo(routes.gallery, { imageId: 2 })}>
          go to picture 2
        </button>
        <button onClick={() => goTo(routes.gallery, { imageId: 3 })}>
          go to picture 3
        </button>
      </div>

      <br/>

      {isRoute(routes.home) && <div>Welcome home</div>}
      {isRoute(routes.about) && <div>About us</div>}
      {isRoute(routes.gallery) && <Gallery />}
    </div>
  );
};
  1. You also get params, queryParams, routeId, path in the router object.
const Gallery = () => {
  const { params } = useRouter();
  return <div>Browsing picture {params.imageId}</div>;
};

FAQ

  • Does it support optional parameters in the path definition?
    Not yet, but it will as soon as I need them in a project.

  • Does it support SSR?
    No.

  • Will it ever support SSR?
    NO.

  • Does it have tests?
    TypeScript is poor man's tests.

  • Will it ever have tests?
    If you write them.

  • Does it support code splitting?
    Did you see which repo you're actually browsing?
    Does it say "facebook" in the url? No. So, no.

  • Does it support async routes?
    Please stop doing stupid stuff with your router.

  • Does it support protected routes?
    Please stop doing stupid stuff with your router.

  • I'm offended by this FAQ section, where can I complain?
    Yell @ me on twitter.com/thekitze

๐Ÿ™‹โ€โ™‚๏ธ Made by @thekitze

  • ๐Ÿซ React Academy - Interactive React and GraphQL workshops
  • ๐Ÿ’Œ Twizzy - A standalone app for Twitter DM
  • ๐Ÿค– JSUI - A powerful UI toolkit for managing JavaScript apps
  • ๐Ÿ“น Vlog - Watch my sad developer life

react-tiniest-router's People

Contributors

kitze avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.