Git Product home page Git Product logo

poormansflux's Introduction

Poor Man's Flux

Build Status

Poor Man's Flux, a really simple React/functional based flux alternative. Read more

The real power of Flux comes from mantaining the unidirectional flow of data from Dispatcher to Actions to Stores to Components.

With poormansflux we keep it simple, just add the poorMansFluxMixin to the top component of your hierarchy.

The poorMansFluxMixin accepts two parameters: Initial Store value and a actions construct (described below)

var
	App,
	poorMansFluxMixin,
	React;
	
React = require('react');
poorMansFluxMixin = require('poormansflux);

App = React.createClass({
	mixins: [poorMansFluxMixin({key: 'value'}, myActions)],
	
	...
});

Actions are defined by a function construct that receives two arguments: a store dispatcher and the flux construct (flux is an object that contains two properties: store and actions)

myActions = function(dispatch, flux) {
  return {
    increaseNumber: function() {
      dispatch({value: flux.store.value + 1});
    },
    decreaseNumber: function() {
      dispatch({value: flux.store.value - 1});
    }
  };
};

In your top component (the one with the mixin) all children components have access to a flux object on this.context (They have to define it to gain access)

var
	Button,
	React;

React = require('react');

Button = React.createClass({
	render: function() {
		<button onClick={this.increase}>{this.context.store.value}</button>
	},
	
	increase: function(evt) {
		this.context.flux.actions.increaseNumber();
	},
	
	contextTypes: {
		flux: React.PropTypes.object
	}
});

As you can see, Action get called, stores get changed, components get redrawn on a circular fashion.

This library was created for academic purpouses, maybe a occasional use on small interfaces where using other Flux libraries would be an overkill.

Updated on version 0.1.5

You can now pass a callback on dispatch as:

myActions = function(dispatch, flux) {
  return {
    increaseNumber: function() {
      dispatch({value: flux.store.value + 1}, function() { ... });
    },
    decreaseNumber: function() {
      dispatch({value: flux.store.value - 1});
    }
  };
};

poormansflux's People

Contributors

ernestofreyreg avatar

Watchers

James Cloos avatar  avatar

Forkers

gitter-badger

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.