Morearty.js
- Introduction
- Download
- Dependencies
- Current status
- Documentation
- Usage
- TodoMVC
- Flux implementation
- React Native support
- Care to help?
- Credits
Introduction
Morearty.js is a thin layer on top of React (implemented as a mixin) providing better state management facilities in the manner of Om but written in pure JavaScript.
Underneath, Morearty leverages immutable data structures, provided by Facebook's Immutable library, which hold the state of an application. That state is described by a single Binding object and all state transitions are performed through it. When an application component needs to delegate a part of its state to a sub-component, it can create a sub-binding which points to a nested location within the global state and is fully synchronized with the original binding. This way every component knows only what it should know and the entire state is effectively encapsulated. Morearty detects any changes automatically and triggers re-rendering. Moreover, each component gets a correctly defined shouldComponentUpdate method that compares the component's state using straightforward JavaScript strict equals operator ===
. So, only the components whose state was altered are re-rendered.
Morearty puts state updates in a render queue and applies them asynchronously in requestAnimationFrame in one pass, falling back to setTimeout
when requestAnimationFrame
is not available. This dramatically simplifies reasoning about the application and improves performance.
See documentation for more info.
Download
Browser, AMD, Node.js environments are supported. You can get production or development versions. Or just npm install morearty
.
Dependencies
Morearty requires React version 0.13 or higher (download) and Immutable 3.7 and above (download). Both should be available as global variables with names React
and Immutable
unless you're using NPM. Require.js users can do something like:
require; ;
Current status
Morearty 0.7 series changes:
- Support React 0.14 (starting from
0.7.25
). - React Native support (thanks to @gilbox).
- Support React 0.13 and Immutable 3.7.
- Asynchronous rendering is the default, synchronous mode is no longer supported.
- Support cancelling transactions.
- Support
this.addBindingListener(...)
in components for component lifecycle bounded listeners creation. Just listen for changes, all required cleanup is performed incomponentWillUnmount
automatically. - Support
renderOnce
configuration parameter useful to ensure rendering is performed only once. Other server rendering corrections. Context.bootstrap
helper method simplifying application bootstrapping.- Support dynamic bindings (#36).
- Support passing custom React context (#37).
- Introduced observed bindings and observed props.
- Support IE8. Deprecate
Binding.delete
in favor ofremove
. - Support
getDefaultMetaState
in components.
Morearty 0.6 series changes:
- React 0.12 and Immutable 3.0 or higher now required.
- Introduce bindings meta info that allows to store data you don't want to put in the main state, e.g. validation info, history, and so on.
- Generate less garbage during render.
- History module migrated on meta binding API.
See releases page for detailed per-release changes descriptions.
Documentation
See Wiki pages for a thourough explanation of Morearty concepts.
Auto-generated API documentation is available here.
Usage
To start using Morearty.js add the script to the page or load it with your favorite AMD loader, e.g. Require.js, and create Morearty context using createContext method:
var Ctx = Morearty;
When you create components this way, each acquires a correctly defined shouldComponentUpdate
method which uses that component's binding (if any) to determine if its state was changed. By default, state is transferred to sub-components in the binding
attribute and can be retrieved using the getDefaultBinding
method.
TodoMVC
To continue this introduction, TodoMVC implementation based on Morearty.js will be used (repository, application). You should have some previous React knowledge to follow painlessly, only Morearty-specific parts will be described.
App component
Let's now define main application module App
:
var NOW_SHOWING = Object; var App = React;
Notice that App
uses getDefaultBinding
method to retrieve its state binding and delegate it to its children. See getDefaultBinding
API doc for the discussion of the default binding concept.
Header component
var Header = React;
In onAddTodo
method component state is updated by appending new TODO item to the list. render
method output custom input
component version suitable for rendering in requestAnimationFrame.
TodoList component
var TodoList = React;
onToggleAll
callback sets completed
property on all items. Note how state is transferred to the children: only the relevant sub-state is passed using sub method which creates a sub-binding pointing deeper into global state. So, TODO item can only access and modify its own cell, and the rest of application state is protected from incidental modification. val method allows to retrieve the value stored in the binding or in its sub-path.
TodoItem
var TodoItem = React;
Here component title is written to the global state using set helper when text in changed. To remove the item no callback needs to be passed from the parent: item component just calls Binding's remove method which removes it from the list of items. In onEnter
method transaction is used to prevent re-rendering between state transitions. It effectively notifies global listeners once on commit.
Footer component
var Footer = React;
Nothing special here so let's jump straight to...
Starting the application
var Bootstrap = Ctx; // will pass root binding to App React;
Ctx.bootstrap
method creates Morearty application bootstrap component which is then passed to React render routine.
Principal differences from raw React
You can compare this Morearty-based TodoMVC implementation to the official React version. Main highlights are:
- No callbacks are passed to sub-components. This becomes especially useful when you find yourself trying to transfer a callback to a component's grand-children (you may never know how your DOM may be restructured after a redesign). There is nothing inherently wrong in passing callbacks to sub-components, but in many cases this can be avoided.
- No hacks in code simulating immutable state and other tricks (look at the comments within React version sources).
- Reasoning about the application is much simpler!
- Each component gets a
shouldComponentUpdate
method, no need to define it manually (but you can if you like). - Less code.
Flux implementation
z3tsu provided Flux version of Todo-MVC based on Reflux: z3tsu/todomvc-morearty-reflux.
React Native support
Starting from version 0.7.16 running on React Native is supported:
var Morearty = ;
Care to help?
Feel free to provide ideas, suggestions, enhancements, documentation improvements. Any feedback or input is highly appreciated. Morearty development is currently driven mostly by feature requests.
Building Morearty
Morearty uses NPM scripts for building: npm run <command>
where command
is one of:
- test (run tests);
- build (run tests and build dist files);
- doc (generate documentation).
Credits
- Alexander Semenov @Tvaroh (author)
- Marat Bektimirov @mbektimirov (collaborator)
- Tim Griesser @tgriesser (collaborator)
- Pavel Birukov @r00ger (collaborator)
- Gil Birman @gilbox (collaborator)
- Valentine Valyaeff @valff (collaborator)