xander
Overview
Frontend Framework for React and Formula.
Usage
Installation
npm install --save react formula xander
Examples
Quick start
A minimal xander app with home and 404 page.
index.js
// Import the boot function to intialize xander.;; // Import style onto the page.; // Define routes for the app.let routes = path: "/" <div>Hello World</div> path: "*" <div>404 NOT FOUND</div> ; // Render your app to the DOM.;
With React's render
Render xander with React's render function.
// Import the boot function to intialize xander.;;;; let App = ;// Define routes for your app.// Boot the app into a root DOM element. Map your URLs to component to render.ReactDOM;
Components
Link Component
A link component to hyperlink your app without annoying page refreshes.
<Link to="/buckets">Go to my buckets</Link>
Eval Component
The Eval component calculates the result of a formula expression.
<Eval exp="SUM(A, B)" values= A: 2 B: 2 />
Rule Component
The Rule component renders HTML describing a formula expression.
<Rule exp="SUM(A, B)" />
Loadable / loader HOCs
The Loadable HOC works with webpack to split your app into chunks that load dynamically.
import loadable from "xander";let routes = path: "/" component: ;
Container Component
The Container component renders the router's current component.
import Link from "xander";;
Connect Component
The Connect HOC component syncs the store with React state.
import connect Container from "xander";;
Stores
Router Store
A minimal router, backed by the history API.
;router;
Use redirect
to modify URL without adding an entry to the history state.
router;
Load routes and related configuration without app
or render
.
;router;
Window Store
The window store keeps track of window size and scroll location; syncs with DOM.
;;
Custom Stores
Create custom stores to store your app's data.
;; // example store, access via the key `todos` in react props.let todosStore = // usagexandertodosStoretodosStoretodosStoretodosStoretodosStoretodosStore
createStore( key, reducerOrSpec, actionsOrSelectors )
A store responds to actions by returning the next state.
const inc = 'inc'; // a simple counting storevar store =
Optionally, you may define a store with a specification.
const inc = "inc";; // a simple counting storevar countStore = ; // object spec makes action creators automatically...countStore;countStore;
Store Properties
Here is a list of store properties that are part of the public API.
name | comment |
---|---|
name | The name of the store |
dispatch | Access to dispatch function |
dispatchToken | A number used to identity the store |
subscribe | A function to tegister a listener |
getState | A function to access state |
setState | Replace the store's state |
replaceReducer | Replace the store's reducer |
dispatch( action )
The entry point to effecting state changes in the app is when an action is dispatch.
Dispatch accepts action as object, promise, or type/data; returns promise.
// Import the dispatch function.var dispatch = // Dispatch action as object // Dispatch action as promise // Dispatch action with type:string and data:object.
WaitFor Example
; // creates a key="A" in the root store, connected to a reducer function.let storeA = ; let storeB = ; // Store with dependencies on state in storeA and storeB.let storeC = ;
getState( )
Returns an object with the store's state by key.
;;
getStores( )
Returns an object with the stores by key.
;;
replaceState( state )
Rehydrate the root state.
; ;
subscribe( listener )
Listen to changes to all stores. This will trigger once each time createStore or dispatch is invoked.
var unsubscribe = subscribe( (state, action) => {
// got change
})
// stop listening
unsubscribe()
Please note that action will be undefined when createStore is invoked.