Giter Club home page Giter Club logo

better-react-native-expo-router's Introduction

better-react-native-expo-router

Just copy the file router.tsx in your project and start using.


Usage

In app/[...container].tsx:

import router from './router';
import { usePathname } from 'expo-router';

router()
  .addRoute('/shop', ShopScreen)
  .addRoute('/shop/{item}', ItemScreen)
  .addRoute('/item-designer', ItemDesignerScreen)
;

// In app/[...container].tsx
export default function Container() {
    const pathname = usePathname();

    return (
        <>
            ...
            {router.execute(pathname, extraProps)}
            ...
        </>
    )
}

In ItemDesignerScreen component's file:

import router from './router';
import { usePathname } from 'expo-router';

router('/item-designer')
    .addRoute('/theme-picker/{color}')
    .addRoute('/pixel-art/{color}')

export default function ItemDesignerScreen() {
    const pathname = usePathname();

    return (
        <>
            ...
            {router('/item-designer').execute(pathname, extraProps)}
            ...
        </>
    )
}

or

import router from './router';
import { usePathname } from 'expo-router';

const r = router('/item-designer')
    .addRoute('/theme-picker/{color}')
    .addRoute('/pixel-art/{color}')

export default function ItemDesignerScreen() {
    const pathname = usePathname();

    return (
        <>
            ...
            {r.execute(pathname, extraProps)}
            ...
        </>
    )
}

Routed components receive the following prop by via the router, and any extra props you pass:

export type RoutableComponent = (props: {routeExecution: RouteExecution}) => JSX.Element

export type Route = {
  pattern: string,
  regexp: RegExp,
  pathParameters: string[],
}

export type RouteExecution = {
  route: Route,
  args: Record<string, string>,
}

Most of expo-router navigation methods supported in all routers:

class Router {
  ...
  navigate(href: Parameters<(typeof expoRouter)['navigate']>[0]) {
    return expoRouter.navigate(href);
  }

  push(href: Parameters<(typeof expoRouter)['push']>[0]) {
    return expoRouter.push(href);
  }

  replace(href: Parameters<(typeof expoRouter)['replace']>[0]) {
    return expoRouter.replace(href);
  }

  back() {
    return expoRouter.back();
  }

  canGoBack() {
    return expoRouter.canGoBack();
  }
}

better-react-native-expo-router's People

Contributors

kapv89 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  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.