Simplux
A Simple Flux Library
Simplux is just a flux library stripped to the bone. Nothing fancy and it's not designed to solve every problem.
It comprises of a series of classes that you can extend to create Stores or Actions.
It comes also with a "Higher Order Component", StoreEnhance, that you can use to inject the Store's data in your views.
Ultimately there is a basic Inflator singleton that allows you to fill the stores both server side and client side, that allows you to easily create an isomorphic app.
Action
Very easily create a Action object with an enum which defines the events your Action can emit
import { Action } from 'simplux';
export default new Action ({
userEvent: 0,
otherEvent: 1
});
Store
To create your store, simply extend the base store.
import { Store } from 'simplux';
import myActions from '../actions/myActions.js';
class MyStore extends Store {
init () {
myActions.on(myActions.EVENTS.userEvent, this.onUserEvent.bind(this));
}
inflate (pageData) { }
onUserEvent (data) {
this.data.myProp1 += 1;
this.done();
}
}
export default new MyStore({
myProp1: 1,
myProp2: 'other prop'
});
StoreEnhance
Once you have created your stores, use the StoreEnhance to easily wrap your views with the data from your stores
import React from 'react';
import { StoreEnhance } from 'simplux';
const stores = {
myStore: require('../stores/myStore.js')
};
const MyView = React.createClass({
render: function () {
return (
<div>
<h1>This is the data from myStore</h1>
myProp1 {this.props.myStore.myProp1}
</div>
);
}
});
export default StoreEnhance(React, MyView, stores);
Inflator
Inflator is a very basic utility that allows you to clean and inflate your stores both on the server and on the client.
On the server, just before calling the "React.renderToString" function, import the Inflator and call the "inflate" function
import React from 'react';
import { Inflator } from 'simplux';
import myView from '../application/myView.js';
export default function handler (req, res) {
var data = {
user: {}
friends: []
};
Inflator.inflate(data);
var viewContent = React.renderToString(React.createElement(myView));
var pageContent = '<div id="page">'
pageContent += viewContent;
pageContent += '</div><script type="application/json" id="page-data">' + JSON.stringify(data) + '</script>';
res.render('template', {pageContent: pageContent});
}
At this point your client side script can leverage the inflator function and it will pre-fill the stores on the browser.
import React from 'react';
import { Inflator } from 'simplux';
import MyView from './myView.js'
Inflator.inflate();
React.render(<MyView />, document.getElementById('page'));
This is still a work in progress. I use it for some of my prototypes, it allows me to easily spin up an isomorphic app with React.
Please feel free to grab it and try it out and feedback via the "issues".