Wickedly quick, stunningly simple, reactive state management.
FNX aspires to be a rock-solid, production-ready, state management solution. Currently, however,
FNX is in its early stages. Until v1.0.0
don't use it for anything critical. You can
help FNX reach v1.0.0
by asking questions, finding bugs, and suggesting improvements.
Getting Started
Install
NPM Registry
yarn add fnx
or npm install fnx --save
UMD Build
<script src="https://unpkg.com/fnx@[VERSION]/umd/fnx.min.js"></script>
<script src="https://unpkg.com/fnx@[VERSION]/umd/ReactiveComponent.min.js"></script>
(Make sure you replace "[VERSION]" with the NPM version of FNX you'd like to use - e.g. "0.0.32")
Starter Projects
Introduction
FNX is a robust state management library optimized for ease of use. It's kinda like Redux met MobX and had a baby. If you're familiar with either solution (or both) FNX shouldn't be too hard to grasp. FNX takes the best ideas from both these libraries and adds a few of it's own. What you end up getting is transparently reactive state management complete with:
- immutable serializable snapshots,
- efficient derived properties,
- support for observing complex properties (such as native
Date
objects), - free runtime type-checking,
- and a powerful middleware api to top it off.
The icing on the cake is easy integration with React through the ReactiveComponent api. In a way, FNX is kinda like React, but for data. It abstracts away the tedious parts of state management (like serialization and keeping your view in sync with your model) leaving you with more time to build awesome stuff.
As the new kid on the block, FNX makes no compromises in order to support older JavaScript environments. It started as part of an academic research project at Brigham Young University and legacy compatibility simply isn't part of the project's goals. Proxies, WeakMaps, and Symbols are among the ES6 features FNX uses internally to provide the best state management solution possible. The good news is that the vast majority of JavaScript runtime environments support these features today (think pretty much everything minus IE11 and older). The bad news is that if you need support for older environments FNX isn't for you.
Simple Example
Here's a basic React app using most of FNX's features. Checkout the
TypeScript setup page or
Babel setup page to learn how to properly configure your
project to work with FNX. ReactiveComponent
and all properties of fnx
are documented in the API
Reference section.
let nextTodoId = 0 // Describe the state tree Model @fnxreadonly id = fnxnumber text = fnxstring completed = fnxboolean @fnxaction { thiscompleted = !thiscompleted } Model filter = fnxstring todos = fnx @fnxcomputed { const todoArray = Object if thisfilter === 'completed' return todoArray else if thisfilter === 'uncompleted' return todoArray else return todoArray } @fnxaction { thisfilter = filter } @fnxaction { thistodosnextTodoId = id: nextTodoId text completed: false nextTodoId++ } // Initialize the state tree const app = filter: 'none' todos: app { console } // Components const Todo = const TodoList = { return <div> <input ref= thisinputEl = c /> <button onClick= this >Add</button> </div> } { app thisinputElvalue = '' } { return <div> Filter <select value= appfilter onChange= this > <option value='none'>None</option> <option value='completed'>Completed</option> <option value='uncompleted'>Uncompleted</option> </select> </div> } { app } const App = // Render the app ReactDOM
Transparent Reactive Programming?
If that sounds hard and confusing to you, rest assured you are not alone. The good news is that it's really a lot simpler than it seems. In reactive programming, reading data is the same as subscribing to change notifications. You don't need to explicitly declare you want to rerun a function when something changes – the function will run again automatically.
There are a lot of libraries out there designed to make reactive programming easier. One such library is RxJS. Libraries like RxJS, however, are not transparent. The transparent part means you can strip away all the fancy API calls typically needed to make reactive programming work. ES6 Proxies enable FNX to apply all of those methods behind the scenes without you needing to grok an entirely new API just to perform simple mutations. Transparent reactive programming gives you the advantages of reactive programming without the obtuse APIs that often accompany it. Actually, you won't need to change hardly anything about how you program. Simply mutate data inside of FNX actions. Transparency means you can use a utility library like Lodash with no issues. Reactivity means FNX is silently taking notes of what's going on and making sure your computed values and view are kept up-to-date with your data.
Contributing
The biggest way to contribute to FNX currently is through feedback. The API is still in flux and the internals are likely to undergo a major rewrite soon. The current version of the source code is a good first draft but needs restructuring to allow some performance enhancements and to make it easier to contribute to. Join the gitter channel or open an issue to give feedback or ask questions. Suggestions and new ideas are welcome!