npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-composite-router

v3.0.0

Published

predictable react router

Downloads

27

Readme

npm npm Dependency Status devDependency Status peerDependency Status

react-composite-router

Create predictable ui composition by declaration of the routes tree.

Without Url sharing in any Link or other places. Url must be hidden!

$ npm install react-composite-router --save
import React from 'react';
import { render } from 'react-dom';
import { Router, Slot, Link, routesTree } from 'react-composite-router';
import createBrowserHistory from 'history/createBrowserHistory';



const App = () => (
  <div>
    <h1>App!</h1>
    <Link state="app.settings" params={{ id: 123 }}>Settings</Link>
    <Slot name="appBody" />
  </div>
)

const SettingsBody = ({ timeStamp, id }) => (
  <div>Settings Body! {id}:{timeStamp}</div>
);



// define the routes tree
const tree = routesTree();

const appRoute = tree.createRootRoute('app', {
  url: '/?timeStamp',
  slots: { 
    root: App 
  },
  params: {
    timeStamp: Date.now() // default value
  }
});

appRoute.createChildRoute('app.settings', {
  url: '/settings/:id',
  slots: { 
    appBody: SettingsBody 
  }
});




const history = createBrowserHistory({ basename: '/app' });

render(<Router history={history} routes={routesTree.getRoutes()}><Slot name="root" /></Router>, document.getElementById('root'));

<Router />

Router.propTypes = {
  children: PropTypes.node.isRequired,
  onChange: PropTypes.func,
  routes: routesPropTypes, // routes by name (returns from routesTree.getRoutes())
  history: PropTypes.object.isRequired // instance of history.js
};

history - instance of history.js

<Link />

Link.propTypes = {
  state: PropTypes.string.isRequired, // state name
  reset: PropTypes.bool, // set true, if you want to reset inherited params for transition 
  params: PropTypes.object.isRequired, // stateParams
  reload: PropTypes.bool.isRequired, // if it is true page will be reloaded after click  
  replace: PropTypes.bool.isRequired, // history.replace
  onClick: PropTypes.func,
  children: PropTypes.any.isRequired,
  disabled: PropTypes.bool.isRequired,
  className: PropTypes.string,
  activeStateClass: PropTypes.string,  // when state or child of this state will be active
  activeClass: PropTypes.string, // only if this state will be active
  disabledClass: PropTypes.string // only if link is disabled
}

<Redirect />

Redirect.propTypes = { // the same description as Link has 
  state: PropTypes.string.isRequired,
  reset: PropTypes.bool,
  params: PropTypes.object,
  reload: PropTypes.bool,
  replace: PropTypes.bool
}

<Slot />


Slot.propTypes = {
  name: PropTypes.string.isRequired, // name of slot
  props: PropTypes.object, // props for component
  render: PropTypes.func, // render function (Component, props, stateParams, stateName)
  children: PropTypes.oneOfType([ PropTypes.func, PropTypes.node ]) // fallback children (if slot will be empty in composition). function (props, stateParams, stateName)
};

routesTree().createRootRoute(name, definition)

routesTree().createRootRoute(name, definition).createChildRoute(childName, definition);

name - String - required

const definition = {
  url: '/some/path/:id', // segment of url,
  params: {}, // default params
  slots: { // slots by name
    someSlotName: Component 
  }
};

usage

import { routesTree } from 'react-composite-router';

const tree = routesTree();

const route = routesTree.createRootRoute('some', { url: '/some' });

route.createChildRoute('some.child', { url: '/child' }); // create an child. name must starts from parent name

HOCs

referring('_statePropName', mapPropsFunc)(Component)

Provide possibility to create your own link or button.

New Component will recognize additional props like:

mapPropsFunc = (props) => ({
  state: PropTypes.string.isRequired,
  reset: PropTypes.bool,
  params: PropTypes.object,
  reload: PropTypes.bool,
  replace: PropTypes.bool
})
let MyComponent = ({ state, children, ...otherProps }) => (
  <div {...otherProps} data-href={state.href} onClick={() => state.apply()}>{children}</div>
);

MyComponent.propTypes = {
  state: PropTypes.shape({
    href: PropTypes.string.isRequired,
    name: PropTypes.string.isRequired, // name of state
    apply: PropTypes.func.isRequired, // function for calling if some event was fired 
    params: PropTypes.object.isRequired, // state params
    isActive: PropTypes.bool.isRequired, // true if this state is active (exactly)
    isActiveState: PropTypes.bool.isRequired // true if this state or child of this state is active
  })
};

MyComponent = referring()(MyComponent);

<MyComponent state="my-state-name" params={{ someParam: 3 }} reload={true} replace={false} reset={true}/>Some</MyComponent>;

Using with redux

this router provide simple redux-reducer. it useful if you are lazy as me =)


import { createStore, combineReducers } from 'redux';
import routerReducer, { changeStateAction } from 'react-composite-router/lib/redux/reducer';

const store = createStore(
  combineReducers({
    routing: routerReducer   
  })
);

const onStateChange = (name, params) => {
  store.dispatch(changeStateAction(name, params)); // deliberately call store.dispatch or create special handler. up to you
};

render(<Router history={history} onChange={onStateChange} routes={routesTree.getRoutes()}><Slot name="root" /></Router>, document.getElementById('root'));