3.2.2 • Published 1 year ago

@rq/react-easy-router v3.2.2

Weekly downloads
1
License
MIT
Repository
github
Last release
1 year ago

react-easy-router

A simple and lightweight alternative to react-router.

Usage

// Main routes file: routes.js
import user from 'modules/user/routes';
import catalog from 'modules/catalog/routes';

export default {
	...user,
	...catalog
};
// Module route file: modules/user/routes.js

import * as Handlers from './handlers';
import { AppBase } from 'app';

export default {
    UserList: {path: '/user/list', component: Handlers.List, wrapper: AppBase},
    UserView: {path: '/user/:user_id', component: Handlers.View, wrapper: AppBase}
}
// index file
import React from 'react';
import { render } from 'react-dom';
import { createHistory, useBasename } from 'history'
import routes from './routes';

// Redux store, optional
import store from './store';
import { Provider } from 'react-redux';

// Example for historyCallback
import * as actions from 'modules/core/actions/core';

const history = useBasename(createHistory)({
	basename: '/'
});

render((
    <Provider store={store}>
        <Router
            historyCallback={() => {
            	// do something when route changed
            	// Example:
            	store.dispatch(actions.didChangeRoute())
            }}
            history={history}
            notFound={notFound} // optional 404 handler, can be null
            routes={routes}/>
    </Provider>
), document.getElementById('app'));

Build url:

import { Link } from 'react-easy-router';

...

<Link to="UserView" params={{user_id: 1}} className="user-link" {...props}>Text</Link>

Specify url-pattern options for URL matching:

// Module route file: modules/user/routes.js

import * as Handlers from './handlers';
import { AppBase } from 'app';

export default {
    // Recognizes /user/ada.lovelace
    UserView: {path: '/user/:name', options: {segmentValueCharset: '.a-z'},
        component: Handlers.View, wrapper: AppBase},
}

Changelog

3.2

  • Replaced componentWillMount usage with constructor and componentWillReceiveProps with componentDidUpdate for React 17 compatibility.
3.2.2

1 year ago

3.2.1

1 year ago

3.2.0

1 year ago

3.1.3

4 years ago

3.1.2

4 years ago

3.1.1

4 years ago

3.0.10

6 years ago