1.0.0 • Published 7 years ago

react-router-flux v1.0.0

Weekly downloads
41
License
MIT
Repository
github
Last release
7 years ago

Powerful extension for react-router to declare routes.

Installation

Using npm:

$ npm install --save react-router-flux

And then you can import components as follows:

// using an ES6 transpiler, like babel
import { Dispatcher, View, Action, Input } from 'react-router-flux';


// not using an ES6 transpiler
var Dispatcher = require('react-router-flux').Dispatcher;
var View = require('react-router-flux').View;
var Action = require('react-router-flux').Action;
var Input = require('react-router-flux').Input;

Declare route dispatcher

<Dispatcher component={ReactClass}/>

Props

PropertyTypeRequiredDescription
componentReactClassyesA React component

Declare view-state

The declaration can define in Dispatcher inside only

<View path={String}/> 

Props

PropertyTypeRequiredDescription
pathStringyesRoute path

Declare inbound parameter into view-state

The declaration can define in a View inside and in a Dispatcher too, but as default value and can be overridden.

<Input name={String} value={Any}/>

Props

PropertyTypeRequiredDescription
nameStringyesInput property name
valueAnynoInput property value

Note: If the value define as Function you can access to route variables 'params', 'query' and 'state', see below example.

Declare transition

The declaration can define in Dispatcher inside only

<Action on={String} to={String} query={Function|Object} 
                                state={Function|Object} 
                                params={Function|Object}/>

Props

PropertyTypeRequiredDescription
onStringyesEvent ID of the component
toStringyesRedirect Route path
queryFunction or ObjectnoDefine query params for the Route path
stateFunction or ObjectnoDefine route state for the Router
paramsFunction or ObjectnoDefine params for the Route path

How Does It Use?

class TodoMVC extends React.Component {
  render() {
    let {
      filter, /** filter parameter **/
      onFilter, /** to go to /todomvc/active when onFilter({filter: 'active'}) **/
      onFilterNotFound  /** to go to /error/404 **/
    } = this.props;
    //...
  }
};

//store filter variable as path parameter
//  `/todomvc`        ->   <TodoMVC filter="all"/>
//  `/todomvc/active` ->   <TodoMVC filter="active"/>
const mapping_v1 = (
  <Router>
    <Dispatcher component={TodoMVC}>
      {/*as default value*/}
      <Input name="filter" value="all"/>
      
      <View path="/todomvc(/:filter)">
        <Input name="filter" value={({params, query, state}) => params.filter}/>
      </View>
    
      <Action on="filter" to="/todomvc(/:filter)"
              params={({filter}) => { return {filter} }}/>
      <Action on="filterNotFound" to="/error/404"/>
    </Dispatcher>
  </Router>
);

//store filter variable as path parameter v_2
//  `/todomvc`        ->   <TodoMVC filter="all"/>
//  `/todomvc/active` ->   <TodoMVC filter="active"/>
const mapping_v2 = (
  <Router>
    <Dispatcher component={TodoMVC}>
      <View path="/todomvc">
        <Input name="filter" value="all"/>
      </View>
      
      <View path="/todomvc/active">
        <Input name="filter" value="active"/>
      </View>
      
      <View path="/todomvc/completed">
        <Input name="filter" value="completed"/>
      </View>
    
      <Action on="filter" to="/todomvc(/:filter)"
              params={({filter}) => { return {filter} }}/>
    </Dispatcher>
  </Router>
);


//store filter variable as query parameter
//  `/todomvc`               ->   <TodoMVC filter="all"/>
//  `/todomvc?filter=active` ->   <TodoMVC filter="active"/>
const mapping_v3 = (
  <Router>
    <Dispatcher component={TodoMVC}>
      {/*as default value*/}
      <Input name="filter" value="all"/>
      
      <View path="/todomvc">
        <Input name="filter" value={({params, query, state}) => query.filter}/>
      </View>
  
      <Action on="filter" to="/todomvc"
              query={({filter}) => { return {filter} }}/>
      <Action on="filterNotFound" to="/error/404"/>
    </Dispatcher>
  </Router>
);

License

MIT, © 2017 Dmitry Divin.