1.3.1 • Published 3 years ago

react-base-routing v1.3.1

Weekly downloads
40
License
MIT
Repository
github
Last release
3 years ago

NPM

react base routing

Layout based router for react. Used react-router-dom. Layout state is not reset when navigating to pages of the same layout

Installation

npm i react-base-routing

or

yarn add react-base-routing

Usage

  1. Create routes.js with paths schema like this
import { AuthLayout, DefaultLayout } from './layouts';
import { AboutPage, HomePage, LoginPage } from './pages';

export default [
    {
        layout: AuthLayout,
        routes: [
            {
                path: ['/login', '/sign-in'],
                exact: true,
                component: LoginPage,
            },
        ],
    },
    {
        layout: DefaultLayout,
        routes: [
            {
                path: '/',
                exact: true,
                render: () => HomePage({ title: 'HomePage' }),
            },
            {
                path: '/about',
                exact: true,
                component: AboutPage,
            },
        ],
    },
];

Create your layout. Each layout must have children prop to render pages

import React from 'react';

const DefaultLayout = ({ children }) => {
    return (
        <main>
            <h4>Default Layout</h4>
            {children}
        </main>
    );
};

export default DefaultLayout;

Create simple page

import React from 'react';

const LoginPage = () => {
    return <h1>Login Page</h1>;
};

export default LoginPage;
  1. On your App.js include router component
import React from 'react';
import { BaseRouting } from 'react-base-routing';
import routes from './routes';

const App = () => {
    return <BaseRouting routes={routes} />;
};

Advanced

  1. You can handle 404 route by passing notFoundPage prop to base component
<BaseRouting routes={routes} notFoundPage={NotFoundPage} />

404 page not use layout. This is only page

  1. You can use SimpleBaseRouting with simple routing schema (without layouts)

Create simple paths schema like this

import { AboutPage, HomePage, LoginPage } from './pages';

export default [
    {
        path: ['/login', '/sign-in'],
        exact: true,
        component: LoginPage,
    },
    {
        path: '/',
        exact: true,
        render: () => HomePage({ title: 'HomePage' }),
    },
    {
        path: '/about',
        exact: true,
        component: AboutPage,
    },
];

In your App include SimpleBaseRouting instead BaseRouting

import React from 'react';
import { SimpleBaseRouting } from 'react-base-routing';
import routes from './routes';

const App = () => {
    return <SimpleBaseRouting routes={routes} />;
};
  1. You can pass paths array to any Router component
import React from 'react';
import { BaseRouting } from 'react-base-routing';
import routes from './routes';

const App = () => {
    const paths = ['/', '/about'];
    return <BaseRouting routes={routes} paths={paths} />;
};

This will tell the router to show a 404 page if there is no page path in the path array.

Live Demo

See example at this link

Code Example

example

1.3.1

3 years ago

1.3.0

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago