1.2.3 • Published 4 years ago

@kemsu/react-routing v1.2.3

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

React Routing

Routing for React using hooks

Installation

npm install @kemsu/react-routing

Example

See more examples here.

import React from 'react'
import { useRoutes, Link, HistoryManager } from '@kemsu/react-routing'

const routes = {
    '/': '/pattern', // redirect
    '/pattern/:id/*': ({ id }, match) => <div> Id = {id}, Match = {match} </div>, // template with named parameters
    '@^/regexp/([-a-z]+)': (params, match) => <div> Param = {params[0]}, Match = {match} </div> // regexp 
}
// OR
/*
const routes = [
    { path: '/', target: '/pattern/42/tail' },
    { path: '/pattern/:id/*', target: ({ id }, match) => <div> Id = {id}, Match = {match} </div> }, 
    { path: new RegExp('^/regexp/([-a-z]+)'), target: (params, match) => <div> Param = {params[0]}, Match = {match} </div> }
]
*/

export default function Example() {

    const route = useRoutes(routes)   

    return (
        <React.Fragment>
            <nav>
                <Link to='/pattern/42/tail'>Pattern</Link>
                <button onClick={() => HistoryManager.push('/regexp/hello-from-regexp/tail')}>RegExp</button>
            </nav>
            <div>
                {route || <div>Default</div>}
            </div>
        </React.Fragment>
    )
}

MIT Licensed

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago

1.0.0-alpha.0

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago