3.2.1 • Published 4 years ago

oatmilk v3.2.1

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

oatmilk

styled with prettier Github release version Commits since release npm release version

Introduction

oatmilk is a minimal routing library for React. Suspsense ready, SSR friendly, and flexible.

Docs

Instructions

Install

yarn add oatmilk

Wrap your App with your routes

Each route is an object with the required shape:

{
    name: string
    path: path
    view: ReactNode
    onEnter?: function
    onBeforeExit?: function
}

Where path is any default pattern of url-pattern. The 2 optional properties onEnter and onBeforeExit are explained in the transition hooks docs.

You must included a route with a name of notFound since this is the fallback route. The order of the routes does not matter.

import oatmilk from 'oatmilk'

const routes: oatmilk.IRoute[] = [
    {
        name: 'home',
        path: '/',
        // send in any react node
        view: () => <p>The world never says hello back..</p>,
    },
    {
        name: 'user',
        // 'named segment'
        path: '/user/:id',
        // use React.lazy to help with code splitting
        view: React.lazy(() => import('./Pages/User.tsx')),
    },
    {
        name: 'notFound',
        path: '/404',
        // or use @loadable/component for SSR support
        view: loadable(() => import('./Pages/NotFound.tsx')),
    },
]

ReactDOM.render(
    <oatmilk.Provider routes={routes}>
        <App />
    </oatmilk.Provider>,
    document.getElementById('root'),
)

Render a RouterView

The RouterView will render the current route's view.

export function App() {
    return (
        <>
            <Navigation />
            <Header />
            <oatmilk.RouterView />
            <Footer />
        </>
    )
}

Render some links

No need to generate the paths here, oatmilk will find the route and generate a path for you that is derived from the state.

function Navigation() {
    return (
        <>
            <oatmilk.Link routeName='home'>Go home</oatmilk.Link>
            <oatmilk.Link routeName='user' state={{ user: 'bitttttten' }}>
                Go to my page
            </oatmilk.Link>
        </>
    )
}

..and off you go 🎉✨

API

useOatmilk

oatmilk has a React hook useOatmilk that provides routing utilities and information about the current route.

It returns the interface oatmilk.IContext which is:

{
    // used to to change routes
    goTo: (toRouteName: string, toState?: object) => void
    // used to generate the href of a link element of a route
    getHref: (routeName: string, state?: object) => string
    // information about the current route
    route: {
        name: string
        path: string
        view: ReactComponent
    }
    // the current state object
    state: object
}

Transition Hooks

oatmilk has 2 transition hooks: onEnter, and onBeforeEnter. You can use it for the global routing context, and also for a single route's context.

import oatmilk, { IRoute, TRouteState } from 'oatmilk'

export const routes: IRoutes = [
    {
        name: 'home',
        path: '/',
        view: HomePage,
        onEnter: (route: IRoute, state: TRouteState) => {
            console.log('I am called as you enter only the home route')
            ArticlesStore.fetchTrendingArticles()
            TodoStore.fetchTodos()
        },
        onBeforeExit: (route: IRoute, state: TRouteState) => {
            console.log('I am called as you exit only the home route')
        },
    },
    {
        name: 'user'
        path: '/user/:slug',
        view: UserPage,
        onEnter: (route: IRoute, state: TRouteState) => {
            UserStore.fetchUserById(state.id)
        },
    },
]

function onEnter(route: IRoute, state: TRouteState) {
    console.log('I am called as you enter any route')
    analytics.logPageView()
}

function onBeforeExit(route: IRoute, state: TRouteState) {
    console.log('I am called as you exit any route')
}

ReactDOM.render(
    <oatmilk.RouterProvider routes={routes} onEnter={onEnter} onBeforeExit={onBeforeExit}>
        <App />
    </oatmilk.RouterProvider>,
    document.getElementById('root'),
)

Hook callee

Note: This hook callee API is a stand in until async react has been shipped. oatmilk is Suspsense ready and friendly, and once async react has been released it is recommended to use React hook based APIs for data fetching with server side rendering. Since we are not there yet, this is the API we are using temporarily!

oatmilk ships with a way to customise how the above hooks are called with a hookCallee function. Use this method to inject your own callee to completely customise the transition hooks. This can help with server side rendering by passing in a store on the server and client. Learn about that in the server rendering docs.

Decoupled

oatmilk is state first, and everything else is derived from this. The current route is held in state and effects like updating the URL, are managed after the fact.

Decoupling your state and data fetching from your view helps you keep a declarative approach to your codebase. It also makes reasoning and testing simplier, and greatly reduces the complexity of server rendering and pre-fetching data.

Credits

Thanks to probablyup's buttermilk, which was a project I found after writing the first draft of oatmilk which inspired a lot of changes and inspiration.. and of course the name.

Made possible by

3.2.1

4 years ago

3.2.0

4 years ago

3.1.0-beta

5 years ago

3.0.4

5 years ago

3.0.3

5 years ago

3.0.2-beta1

5 years ago

3.0.2

5 years ago

3.0.6-beta4

5 years ago

3.0.6-beta3

5 years ago

3.0.6-beta2

5 years ago

3.0.6-beta

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

3.0.5-beta

5 years ago

3.0.3-beta

5 years ago

3.0.2-beta

5 years ago

3.0.1-beta

5 years ago

3.0.0-beta

5 years ago

2.12.0

5 years ago

2.11.0

5 years ago

2.9.1

5 years ago

2.9.0-beta2

5 years ago

2.9.0-beta1

5 years ago

2.9.0-beta0

5 years ago

2.8.3

5 years ago

2.8.2

5 years ago

2.8.1

5 years ago

2.8.0

5 years ago

2.7.1

5 years ago

2.7.0

5 years ago

2.6.0

5 years ago

2.5.0

5 years ago

2.4.3

5 years ago

2.4.2

5 years ago

2.4.1

5 years ago

2.4.0-beta

5 years ago

2.4.0

5 years ago

2.3.0

5 years ago

2.2.0

5 years ago

2.2.0-beta.2

5 years ago

2.2.0-beta.1

5 years ago

2.2.0-beta.0

5 years ago

2.1.1

5 years ago

2.0.0

5 years ago

1.15.0

5 years ago

1.14.0

5 years ago

1.13.0

5 years ago

1.12.0

5 years ago

1.11.0

5 years ago

1.10.0

5 years ago

1.9.0

5 years ago

1.8.4

5 years ago

1.8.3

5 years ago

1.8.2

5 years ago

1.8.1

5 years ago

1.8.0

5 years ago

1.7.2

5 years ago

1.7.1

5 years ago

1.7.0

5 years ago

1.6.1

5 years ago

1.6.0

5 years ago

1.5.0

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.2

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.1

5 years ago

1.0.0

5 years ago