• Stars
    star
    139
  • Rank 262,954 (Top 6 %)
  • Language
    JavaScript
  • License
    Apache License 2.0
  • Created over 10 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A library combining tools to develop with a FLUX architecture

flux-react

A React JS flux expansion based on experiences building www.jsfridge.com and www.jflux.io. Read more about FLUX over at Facebook Flux. I wrote an article about it here: My experiences building a FLUX application and the background for version 2.6.0 and up is here: An alternative render strategy with FLUX and React JS

What is it all about?

It can be difficult to get going with React JS and FLUX as there is no complete framework with all the tools you need. This project will help you get going with the FLUX parts and it has a boilerplate with a browserify workflow, flux-react-boilerplate.

How to install

Download from releases/ folder of the repo, use npm install flux-react or bower install flux-react, but I recommend using the boilerplate located here: flux-react-boilerplate. It has everything set up for you.

API

flux.debug

var flux = require('flux-react');
flux.debug();

Puts React on the global scope, which triggers the React dev tools in Chrome

flux.createActions()

var flux = require('flux-react');
var actions = flux.createActions([
	'addTodo',
	'removeTodo'
]);
actions.addTodo(); // Trigger action
actions.removeTodo(0); // Pass any number of arguments

Use them inside components or other parts of your architecture. The only way to reach a store is through an action.

flux.createStore()

var flux = require('flux-react');
var MyStore = flux.createStore({});

Creates a store.

state

var flux = require('flux-react');
var MyStore = flux.createStore({
	todos: []
});

Add state properties directly to your store.

actions

var flux = require('flux-react');
var actions = require('./actions.js');
var MyStore = flux.createStore({
	todos: []
	actions: [
		actions.addTodo
	]
});

List what actions the store should handle. They will map to handler with the same name.

handler

var flux = require('flux-react');
var actions = require('./actions.js');
var MyStore = flux.createStore({
	todos: []
	actions: [
		actions.addTodo
	],
	addTodo: function (title) {
		this.todos.push({title: title, completed: false});
	}
});

Based on the name of the action, add a handler that will run when the action is triggered. Any arguments passed to the action will be available in the handler.

events

var flux = require('flux-react');
var actions = require('./actions.js');
var MyStore = flux.createStore({
	todos: []
	actions: [
		actions.addTodo
	],
	addTodo: function (title) {
		this.todos.push({title: title, completed: false});
		this.emit('todos.add');
	}
});

flux-react uses EventEmitter2 which allows for more granular control of events. Instead of always triggering a change event or/and trigger a specific event you will now always trigger a specifically named event. It is recommended to namespace your events related to the state you are changing. In the current example "todos" is the state name and "add" is what we did to that state. That becomes "todos.add". Look at component to see how you can use this to optimize your rendering.

exports

var flux = require('flux-react');
var actions = require('./actions.js');
var MyStore = flux.createStore({
	todos: [],
	actions: [
		actions.addTodo
	],
	addTodo: function (title) {
		this.todos.push({title: title, completed: false});
		this.emit('todos.add');
	},
	exports: {
		getTodos: function () {
			return this.todos
		}
	}
});

Methods defined in exports will be returned by createStore. Components or other parts of the architecture can use it to get state from the store. The methods are bound to the store, meaning "this.todos" points to the state "todos".

Note! Values returned by an export method will be deep cloned. Meaning that the state of a store is immutable. You can not do changes to a returned value and expect that to be valid inside your store also. You have to trigger an action to change the state of a store.

mixins

var flux = require('flux-react');
var actions = require('./actions.js');

var MyMixin = {
	actions: [
		actions.removeTodo
	],
	removeTodo: function (index) {
		this.state.todos.splice(index, 1);
		this.emit('todos.remove');
	}
};

var MyStore = flux.createStore({
	todos: [],
	mixins: [MyMixin],
	actions: [
		actions.addTodo
	],
	addTodo: function (title) {
		this.todos.push({title: title, completed: false});
		this.emit('todos.add');
	},
	exports: {
		getTodos: function () {
			return this.todos
		}
	}
});

Mixins helps you handle big stores. You do not want to divide your stores within one section of your application as they very quickly become dependant on each other. That can result in circular dependency problems. Use mixins instead and create big stores. state, actions, handlers and exports will be merged with the main store as expected.

ProTip! In big stores it is a good idea to create a StatesMixin that holds all possible state properties in your store. That makes it very easy to look up what states are available to you.

var flux = require('flux-react');

var StateMixin = {
	someState: true,
	stateA: 'foo',
	stateB: 'bar',
	stateC: []
};

var MyStore = flux.createStore({
	mixins: [StateMixin, OtherMixin, MoreMixin],
	exports: {
		getSomeState: function () {
			return this.someState;
		}
	}
});

listener

var React = require('react');
var MyStore = require('./MyStore.js');
var MyComponent = React.createClass({
	componentWillMount: function () {
		MyStore.onAny(this.updateState); // On any events triggered from the store
		MyStore.on('todos.add', this.updateState); // When specific event is triggered from the store
		MyStore.on('todos.*', this.updateState); // When events related to todos are triggered from the store
		MyStore.once('todos.add', this.updateState); // Trigger only once
		MyStore.many('todos.remove', 5, this.updateState); // Trigger only 5 times
	},
	componentWillUnmount: function () {
		MyStore.offAny(this.updateState); // Remove all events listener
		MyStore.off('todos.add', this.updateState); // Remove any other type of listener
	},
	updateState: function () {
		this.setState({
		  todos: MyStore.getTodos()
		});
	},
	render: function () {
		return (
			<div>Number of todos: {this.state.getTodos().length}</div>
		);
	}
});

Add and remove listeners to handle updates from the store

RenderMixin

If you want to know more about this feature, read the following article: An alternative render strategy with FLUX and React JS. The main point is that React JS will most likely rerender your whole application on every change event in your stores. You can control this behavior by adding the RenderMixin to your components. Instead of a component automatically rerenders all child components it will only do that if the props or state of the component actually has changed. It is important that you move state from the store to the state of the component, like in the example below. That ensures that the mixin can evaluate the need for a render.

var React = require('react');
var flux = require('flux-react');
var MyStore = require('./MyStore.js');
var MyComponent = React.createClass({
	mixins: [flux.RenderMixin],
	componentWillMount: function () {
		MyStore.on('todos.*', this.updateState);
	},
	componentWillUnmount: function () {
		MyStore.off('todos.*', this.updateState);
	},
	updateState: function () {
	  this.setState({
	    todos: MyStore.getTodos()
	  });
	},
	render: function () {
		return (
			<div>Number of todos: {this.state.todos.length}</div>
		);
	}
});

Add and remove listeners to handle updates from the store

Changes

2.6.4

  • Fixed AMD support (Thanks @VictorBlomberg)

2.6.2

  • Optimized RenderMixin with shallow check

2.6.1

  • Added feature detection on ArrayBuffer, Blob and File. Thanks to @rdjpalmer

2.6.0

  • Added RenderMixin to give you an alternative optimized render strategy

2.5.1

  • Added eventemitter2 to package.json

2.5.0

  • Changed to EventEmitter2 to allow more granular control of changes in store
  • Updated documentation with examples
  • Old API still works

License

flux-react is licensed under the MIT license.

The MIT License (MIT)

Copyright (c) 2014 Christian Alfoni

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

formsy-react

A form input builder and validator for React JS
JavaScript
2,595
star
2

webpack-express-boilerplate

A boilerplate for running a Webpack workflow in Node express
JavaScript
1,398
star
3

webpack-bin

A webpack code sandbox
JavaScript
710
star
4

flux-angular

Use the FLUX architecture with Angular JS
JavaScript
313
star
5

react-app-boilerplate

React application boilerplate
JavaScript
305
star
6

EmptyBox

A complete isomorphic hackable blog service based on React JS
JavaScript
211
star
7

react-webpack-cookbook

Temp repo for transfer
HTML
187
star
8

flux-react-boilerplate

A boilerplate for a full FLUX architecture
JavaScript
166
star
9

react-states

Explicit states for predictable user experiences
TypeScript
139
star
10

markdown-to-react-components

Convert markdown into react components
JavaScript
132
star
11

rxjs-react-component

A component allowing you to change state using observables
JavaScript
119
star
12

create-ssl-certificate

Command line tool to create self signed SSL certificate
JavaScript
97
star
13

immutable-store

An easy to use immutable store for React JS and FLUX architecture
JavaScript
90
star
14

impact

Reactive state management for React
TypeScript
86
star
15

webpack-example

An example of webpack workflow
JavaScript
78
star
16

proxy-state-tree

An implementation of the Mobx/Vue state tracking approach, for library authors
JavaScript
66
star
17

timsy

Agnostic functional state machine with epic type support
TypeScript
58
star
18

redux-nodes

Nodes instead of reducers
TypeScript
47
star
19

react-simple-flex

An intuitive abstraction over flexbox
JavaScript
47
star
20

reactive-router

A reactive wrapper around Page JS
JavaScript
46
star
21

react-addressbar

Take control of the addressbar in a reactive way
JavaScript
42
star
22

form-data-to-object

Converts application/x-www-form-urlencoded keys to plain JS object
JavaScript
36
star
23

react-packaging

Examples of how to create a workflow with React JS using different packaging tools
JavaScript
31
star
24

jflux

An easy to use unidirectional component based framework
JavaScript
29
star
25

objectory

Object factory honoring JavaScripts prototype delegation
JavaScript
27
star
26

npm-extractor

Extracts npm packages into memory
JavaScript
18
star
27

emmis

Create a chaining API for your application
TypeScript
17
star
28

isomorphic-react-baobab-example

An example related to article about using baobab to create an isomorphic react app
JavaScript
17
star
29

process-control

A tool for managing continuous async process by starting, stopping, restarting and disposing
TypeScript
17
star
30

flutter_observable_state

Observable state for flutter applications
Dart
16
star
31

angular-flux

A plugin for Angular JS that lets you write code with the FLUX pattern
JavaScript
16
star
32

exploring-elm-boilerplate

A boilerplate used in an article series exploring Elm
JavaScript
14
star
33

baobab-hot-loader

Using Webpack hot replacement update your application state without reload
JavaScript
13
star
34

predictable-user-experiences-in-react-book

A book about predictable user experiences in React
TypeScript
13
star
35

cerebral-react-baobab

A Cerebral package with React and Baobab
JavaScript
13
star
36

op-op-spec

The op-op specification. A simple straight forward approach to functional programming
13
star
37

flux-react-router

A router in the flux-react family
JavaScript
13
star
38

signalit

Manage state locally and globally in React with signals
TypeScript
13
star
39

observable-state

Observables for creating reactive functional state in applications
JavaScript
11
star
40

cerebral-react-immutable-store

Cerebral package with react and immutable store
JavaScript
10
star
41

R

An application framework using functional reactive concepts and virtual-dom
JavaScript
9
star
42

flux-react-dispatcher

React dispatcher for the FLUX architecture
JavaScript
9
star
43

backbone-draganddrop-delegation

A consistent drag and drop across browsers using native like events
JavaScript
9
star
44

react-environment-interface

Define and consume a custom environment for your application
TypeScript
8
star
45

baobab-angular

A state handling library for Angular, based on Baobab
JavaScript
8
star
46

react-nodesy

React node hierarchy with render props
TypeScript
7
star
47

chrome-recorder

Lets you easily record audio and video in Chrome
JavaScript
7
star
48

cerebral-angular-immutable-store

A cerebral package for angular and immutable-store
JavaScript
7
star
49

reactive-app

Build large scale applications supported by visual tools
TypeScript
5
star
50

virtual-dom-loader

A virtual-hyperscript, used in virtual-dom, to JSX converter for Webpack
JavaScript
5
star
51

HotDiggeDy

Test project for using observables to build complex apps
JavaScript
5
star
52

typed-client-router

TypeScript
4
star
53

batchcalls

Batches calls to a function and passes arguments as arrays
JavaScript
4
star
54

cerebral-boilerplate

A webpack boilerplate for cerebral applications
JavaScript
4
star
55

class-states

Manage async complexity with states
TypeScript
4
star
56

cerebral-router-demo

The demo code for the cerebral router introduction
JavaScript
4
star
57

actorial

Actor pattern with state machines
TypeScript
4
star
58

flux-react-store

A simple construct for your flux stores
JavaScript
4
star
59

create-gql-api

Simplify GQL typing and consumption
JavaScript
4
star
60

christianalfoni.com

My personal website
TypeScript
4
star
61

redux-proxy-thunk

TypeScript
3
star
62

TeachKidsCode

An open source service for learning code
JavaScript
3
star
63

formsy-angular

A form input builder and validator for Angular JS
JavaScript
3
star
64

the-angular-experiment

An experiment bringing components and immutable state to Angular
JavaScript
3
star
65

grunt-tdd

Run browser and Node JS tests on Buster, Mocha or Jasmine and get the reports in the browser
JavaScript
3
star
66

boilproject-service

The service that allows you to create boilerplates
TypeScript
2
star
67

new-git-flow

Created with CodeSandbox
HTML
2
star
68

cerebral-immutable-store

Immutable Store Model layer for Cerebral
JavaScript
2
star
69

family-scrum-v2

TypeScript
2
star
70

webpack-express-isomorphic-react-boilerplate

An isomorphic boilerplate with react, babel, routing and express on the server
2
star
71

middleend-boilerplate

A boilerplate for building applications with a middleend
2
star
72

markdown-excalidraw

TypeScript
2
star
73

codesandbox-weekplanner

Created with CodeSandbox
TypeScript
2
star
74

lib-boilerplate

A boilerplate for creating any JavaScript frontend, or frontend+backend, library
JavaScript
2
star
75

models-test-2

Created with CodeSandbox
JavaScript
1
star
76

grunt-buster-tdd

A browser and Node TDD tool using Buster JS
JavaScript
1
star
77

devchat

TypeScript
1
star
78

C

An inheritance experiment which allows for private variables and "super" up the constructor chain
JavaScript
1
star
79

boilproject-cli

An NPX tool to easily start a new project
JavaScript
1
star
80

Protos

Inheritance the native JavaScript way
JavaScript
1
star
81

our-project-design-system

Created with CodeSandbox
JavaScript
1
star
82

hm

1
star
83

create-css-theme

Converts your theme into css variables for easy consumption with CSS approach
1
star
84

contribute-vscode-plugin

Contribute to github for beginners
TypeScript
1
star
85

module-loader-tdd

An easy to use and easy to test module loader
JavaScript
1
star
86

validate

A general JavaScript validation tool with pre-configuration, Backbone support and can be used cross client/server
1
star