A simple library for uni-directional dataflow application architecture with React extensions inspired by Flux
Installations
npm install reflux
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
6.5.0
NPM Version
3.10.3
Score
58.2
Supply Chain
96.1
Quality
78.8
Maintenance
50
Vulnerability
99.6
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
reflux
Download Statistics
Total Downloads
11,548,545
Last Day
2,394
Last Week
28,596
Last Month
154,978
Last Year
2,546,897
GitHub Statistics
5,357 Stars
579 Commits
328 Forks
134 Watching
3 Branches
63 Contributors
Bundle Size
20.60 kB
Minified
5.97 kB
Minified + Gzipped
Package Meta Information
Latest Version
6.4.1
Package Id
reflux@6.4.1
Size
64.32 kB
NPM Version
3.10.3
Node Version
6.5.0
Publised On
28 Feb 2017
Total Downloads
Cumulative downloads
Total Downloads
11,548,545
Last day
-68.8%
2,394
Compared to previous day
Last week
-35.7%
28,596
Compared to previous week
Last month
-3.1%
154,978
Compared to previous month
Last year
88%
2,546,897
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
2
Peer Dependencies
1
Dev Dependencies
25
RefluxJS
A simple library for unidirectional dataflow architecture inspired by ReactJS Flux.
Installation
You can currently install the package as a npm package, a bower component, or import it from a CDN.
NPM
The following command installs RefluxJS as a npm package:
npm install reflux
Then, in your script, you can gain a reference to RefluxJS like so: var Reflux = require('reflux');
Bower
The following command installs reflux as a bower component that can be used in the browser:
bower install reflux
Then the files may be imported into your html file via bower_components/reflux/dist/reflux.js
or bower_components/reflux/dist/reflux.min.js
. At that point a Reflux
variable will be globally available to you. It is suggested that you import RefluxJS after React.
CDN
RefluxJS is available at jsdelivr.
You may import the CDN files directly through a script tag. At that point a Reflux
variable will be globally available to you. It is suggested that you import RefluxJS after React.
Overview
The main function of Reflux is to introduce a more functional programming style architecture by eschewing MVC like pattern and adopting a single data flow pattern.
+---------+ +--------+ +-----------------+
¦ Actions ¦------>¦ Stores ¦------>¦ View Components ¦
+---------+ +--------+ +-----------------+
^ ¦
+--------------------------------------+
The pattern is composed of actions and data stores, where actions initiate new data to pass through data stores before coming back to the view components again. If a view component has an event that needs to make a change in the application's data stores, they need to do so by signaling to the stores through the actions available.
Usage
For usage, you need to create actions which can be called from React components. Those actions are listened to by stores which hold and update data. In turn those stores are hooked up to React components and set state within them as it is updated within the store.
Therefore the 3 main concepts to know are:
Creating Actions
Create an action by calling Reflux.createAction
with an optional options object.
1var statusUpdate = Reflux.createAction();
An action is a function object that can be invoked like any other function.
1statusUpdate(data); // Invokes the action statusUpdate
There is also a convenience function for creating multiple actions.
1var Actions = Reflux.createActions([ 2 "statusUpdate", 3 "statusEdited", 4 "statusAdded" 5]); 6 7// Actions object now contains the actions 8// with the names given in the array above 9// that may be invoked as usual 10 11Actions.statusUpdate();
More on Actions:
Actions can also:
- load files asynchronously with child actions
- do preEmit and shouldEmit checking
- have many shortcuts for easy usage
See Reflux Action Documentation for more.
Creating Stores
Create a data store much like ReactJS's own React.Component
by creating a class extending Reflux.Store
. The store has a state
property much like a component, and uses setState
like a component as well. You may set up all action listeners in the constructor
and register them by calling the store's own listenTo
function.
1class StatusStore extends Reflux.Store 2{ 3 constructor() 4 { 5 super(); 6 this.state = {flag:'OFFLINE'}; // <- set store's default state much like in React 7 this.listenTo(statusUpdate, this.onStatusUpdate); // listen to the statusUpdate action 8 } 9 10 onStatusUpdate(status) 11 { 12 var newFlag = status ? 'ONLINE' : 'OFFLINE'; 13 this.setState({flag:newFlag}); 14 } 15}
In the above example, whenever the action statusUpdate
is called, the store's onStatusUpdate
callback will be called with whatever parameters were sent in the action. E.g. if the action is called as statusUpdate(true)
then the status
argument in the onStatusUpdate
function is true
.
Stores also integrate easily with sets of actions via things like this.listenables
. When an actions object (or an Array of multiple actions objects) is applied to this.listenables
you may automatically add listeners simply by naming convention. Just name the functions either after the action name (such as actionName
, or the camelcased action name preceded with "on", (such as onActionName
).
1var Actions = Reflux.createActions(['firstAction', 'secondAction']); 2 3class StatusStore extends Reflux.Store 4{ 5 constructor() 6 { 7 super(); 8 this.listenables = Actions; 9 } 10 11 onFirstAction() 12 { 13 // calls on Actions.firstAction(); 14 } 15 16 onSecondAction() 17 { 18 // calls on Actions.secondAction(); 19 } 20}
More on Stores:
Reflux stores are very powerful. They can even do things like contribute to a global state that can be read and set for partial or full-state time-travel, debugging, etc.
See Reflux Store Documentation to learn more about stores.
Hooking Stores to Components
Once you've created actions and stores, now the last step in working RefluxJS is to hook those stores to a React component.
This is done as simply as extending Reflux.Component
instead of React.Component
and setting the store(s) to use. Reflux.Component
itself extends React.Component
, so you use them the exact same. The only difference is that Reflux.Component
allows you to set stores for the component to get state from:
1class MyComponent extends Reflux.Component 2{ 3 constructor(props) 4 { 5 super(props); 6 this.state = {}; // our store will add its own state to the component's 7 this.store = StatusStore; // <- just assign the store class itself 8 } 9 10 render() 11 { 12 var flag = this.state.flag; // <- flag is mixed in from the StatusStore 13 return <div>User is {flag}</div> 14 } 15}
When the component mounts it will either create a singleton instance of StatusStore
(if one isn't already made) or use an already made singleton (if it was already created by another component that uses the store).
Of important note is that you can:
- Set multiple stores by setting
this.stores
(the plural) and setting it to an Array of store classes. - Set a
this.storeKeys
Array to restrict only certain parts of the store being mixed into the component state.
There is also a mapStoreToState
method in the documentation for those that want absolute control over how a store's state is mapped to a component.
1class MyComponent extends Reflux.Component 2{ 3 constructor(props) 4 { 5 super(props); 6 this.state = {type:'admin'}; // <- note that we can still use normal state 7 this.stores = [StatusStore, AnotherStore]; 8 this.storeKeys = ['flag', 'info']; 9 } 10 11 render() 12 { 13 var flag = this.state.flag; 14 var info = this.state.info; 15 var type = this.state.type; 16 return <div>User is {flag}, info: {info}, type: {type}</div> 17 } 18}
The above will mix in properties from the state of both StatusStore
and AnotherStore
. However, because of this.storeKeys
it will only mix in the properties flag
and info
from them. So any other properties within those stores will not get mixed in. So even if a store contained a type
property in its state it would not get mixed in, and the type
value we set as a normal part of the component state is safe.
More on using Reflux style components:
Reflux's simple and intuitive way of integrating stores into components is easy and powerful. You can aggregate stores together on a component-by-component basis, filter which parts of the stores come through and which don't, or even do a detailed manual mapping of exactly how you want the state from stores to map to the state in a particular component.
See Reflux Style Component Documentation to learn more.
Documentation
What you've just read is a "view from 10,000 feet" type overview of getting started with RefluxJS. For serious learning see the documentation.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE.md:0
- Info: FSF or OSI recognized license: BSD 3-Clause "New" or "Revised" License: LICENSE.md:0
Reason
Found 3/23 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 12 are checked with a SAST tool
Score
3.2
/10
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to reflux
js-message
normalized JS Object and JSON message and event protocol for ES6+ node.js, browsers, electron, vanialla js, react.js, components, actions, stores and dispatchers
reflux-core
A simple library for uni-directional dataflow application architecture inspired by ReactJS Flux
hadron-reflux-store
Hadron Reflux Stores
@types/reflux
TypeScript definitions for reflux