top | item 8923053

Marty.js – A JavaScript library for state management in React applications

255 points| jfhollingworth | 11 years ago |martyjs.org | reply

93 comments

order
[+] hakanderyal|11 years ago|reply
Nice documentation, and dev tools is a big plus.

The beauty of flux + react is, it's so simple and flexible.

I'm developing a fairly complex application with flux + react, (no flux frameworks, just facebook's dispatcher and stores/actions modelled after facebook's flux examples), and it seems I've implemented lots of things in marty.js. I also started using mixins for subscribing views to stores, immutable data in stores (so undo/redo is available without much work), loading data into stores from different sources etc.

I believe, in the following months, a de-facto flux framework will emerge, and marty looks so close to being that framework.

A few tips to fellow developers want to try react and/or flux:

- React is just the view part. Integrating it with other frameworks is easy, and integrating other js libs with react is also easy.

- OTOH, if you want to build a whole complex app with react, use something like flux with it, since there isn't a flexible way to modify the app state from child components with react.

- Immutable data structures really goes well with react + flux. I haven't been able to get the immutability/functional style before, and react + flux really helped with that.

Thanks again for the great tools.

[+] eric_bullington|11 years ago|reply
Great comment and excellent intro to React for interested devs. I've been successfully using React for small and mid-sized apps, and agree that Marty looks like it's an elegant solution to the Flux pattern. (for application state in React, I've also had some success with passing around an immutable cursor, like react-cursor[1]).

One cautionary note about how easy it is to integrate React with other frameworks: this statement is true as long as the framework in question doesn't touch the DOM (e.g., using Backbone models). But if you try to integrate React with any JS library or framework that mutates the DOM, you should be aware that this will interfere with React's DOM "accounting" and potentially cause lots of problems.

There are certainly ways around this (look into the lesser-used React life cycles), but it's much easier to stick within the React ecosystem for any libraries that manipulate the DOM, or build your DOM manipulations from scratch (easy to do with React). Already, there's a fairly feature complete React Bootstrap library [2]. I've started work on a React charting library, using d3 for path calculations and other utilities (and temporarily for chart axes generation, but library users shouldn't have to worry about any DOM issues) [3]. And I'm sure there are a thousand and one other React view-level component libraries I'm not familiar with, most of which are likely available via NPM.

But I agree that React and the other virtual DOM libraries and frameworks (Ractive, Mercury, Om, etc.) are the next generation approach for client-side views. They not only make DOM manipulations much snappier, but they also make them much easier for the developer.

1. https://github.com/dustingetz/react-cursor

2. https://github.com/react-bootstrap/react-bootstrap

3. https://github.com/esbullington/react-d3

[+] goatslacker|11 years ago|reply
Curiously, what about marty do you really like that makes it feel like a good de-facto framework?
[+] colinramsay|11 years ago|reply
I'm scanning the documentation for this now and the "state sources" concept looks useful. Stores seem to be a bit cleverer than in other Flux implementations too.

The fact it's got a Chrome extension and what appears to be comprehensive documentation bodes well. I'm currently using reflux because I like its simplicity (no dispatcher and no action creators) but Marty has definitely piqued my interest.

Edit: Question for the author. Any thoughts on isomorphic apps with Marty? Edit2: Answering my own question: https://github.com/jhollingworth/marty/issues/13

[+] jfhollingworth|11 years ago|reply
Isomorphic apps are the next big challenge. Right now the biggest blocker is that everything's a singleton. I'm working on an internal container which should hopefully mean existing apps can be made isomoprhic without a change to the API
[+] glifchits|11 years ago|reply
General impression is the same. I'm using Reflux and quite happy with it, but this does seem pretty smart. Having built in rollback of actions is nice, and having the versatility of where-queries to invoke action handlers is interesting. Not sure if its worth the switch, there aren't really pain points to be solved here... but my next React project might use this.
[+] daveidol|11 years ago|reply
One thing I would love to see some guidance on with Flux (and potentially with the patterns expressed in Marty.js) is how to handle the use case of Stores for a collection of items as well as a single item.

Right now, if I create a Store for a collection of Users and I want to be able to mutate these User objects then I create actions for each CRUD mutation and then plumb that change through to the in-memory object inside my collection as well as persist the change to my server.

But as soon as I click on a specific User and load up a new page for that User, for example, then I need to create an all new Store just for a single User, which has all of the same mutations/actions available to it as the previously-mentioned collection Store (except now I don't need to pass the ID to every action payload).

The best approach I've seen on this topic is in this example's Readme: https://github.com/gaearon/flux-react-router-example

Does this approach make sense/jive with the Marty.js way of handling Stores, State Sources, etc?

[+] hakanderyal|11 years ago|reply
The method that you linked is the one I use currently.

Another way is to use a nested immutable data structure (something like immutable.js[1], react-cursor[2]), use only one store, and make all changes on this data structure and pass accordingly.

Even if you don't use immutable.js, I strongly suggest to use immutability with react update[3] or equivalent. Things get way easier that way.

[1]: https://github.com/facebook/immutable-js

[2]: https://github.com/dustingetz/react-cursor

[3]: https://facebook.github.io/react/docs/update.html

[+] peterjmag|11 years ago|reply
Out of curiosity, for those of you building stuff with Flux: Which library do you use? Or do you just use your own implementation?

We recently went with Reflux[1] for our first big React app after reading about it here[2], and I've been pretty happy with it so far.

[1] https://github.com/spoike/refluxjs

[2] https://reactjsnews.com/the-state-of-flux/

[+] robertfw|11 years ago|reply
I've been using Fluxxor http://fluxxor.com/ for a relatively large application. Has been working well! When I started the project the flux concept was still very, very new and there were not many implementations. It took care of the basics and has gotten out of my way. I'm looking forward to trying marty out on a new app.
[+] goatslacker|11 years ago|reply
I started with reflux but wanted something that was actual flux. So I ended up with

https://github.com/goatslacker/alt

which looks a bit like reflux in terms of terseness but it adds the ability to have snapshots and rollbacks, isomorphism, and it plays real well with ES6.

There's a few of the flux examples that compare flux to alt in the repo. And there are some good isomorphic examples in here https://github.com/goatslacker/iso/tree/master/examples

I'll be posting a write-up of all this pretty soon.

If you like reflux you'll like alt.

[+] bsimpson|11 years ago|reply
I'm using Reflux for now, though I'm tempted to switch to something more FRP like Bacon, f, or RxJS. I find that I end up making stores for both AllThings and CurrentThing, and it should be easier to express that relationship with FRP.
[+] mjohn|11 years ago|reply
I really like Reflux as well. In a recent project I used Bacon.js[1] to create my own implementation that ended up looking similar to Reflux.

The project involved asynchronous requests triggered by the actions, and transforming & aggregating the returned data. Compared to Reflux, Bacon made it very easy to do this by composing small functions that are applied sequentially to a stream of actions.

[1] https://baconjs.github.io/

[+] rok3|11 years ago|reply
I'm leading a mid-sized project that is using RefluxJS. We're very happy with it.
[+] rattray|11 years ago|reply
This looks great. The docs really are terrific, too.

Personally, I've been playing around with Bacon.js (or RxJS) instead of the Flux dispatcher. Using `Bacon.update` in conjunction with Facebook's Immutable.js seems really promising. The outcome is a far more functional approach.

A quick and dirty example: https://gist.github.com/rattrayalex/dee40d86813bcaa9de80

[+] RobertKerans|11 years ago|reply
Thanks for posting that, I've just been trying to get something similar to work using Bacon, and was looking for some examples of how to approach it.
[+] dugmartin|11 years ago|reply
Interesting use of a bus for each action. I'm curious why you used @setProps() instead of just storing the person object in the component state? Modifying props seems a like non "Reactish".
[+] colinramsay|11 years ago|reply
It took me a second for this to click but it looks fantastic. It'd be good to follow this up with a larger example - what are the issues you foresee?
[+] rpwverheij|11 years ago|reply
I'm using react quite a lot, but with my own API / storage solutions. So far I'm not really familiar with flux, and also with this, just quickly looking at it, I don't understand if/why I would need it. I can't really wrap my head around what exactly all this allows me to do and can't bring up the effort to read all the docs. I think I'll read more about flux first at some point, and if I'll ever want to use that, come back and check Marty :) Happy to see active react development though
[+] jasim|11 years ago|reply
In your typical React component, you'll keep state in the parent, and pass them down as props to the children's subtrees. You also have to pass down handlers (`handleNameChange` for example) along with the props so that the child components can change the state. These handlers travel back to the parent, modify the state there, which triggers a re-render, and that travels from the parent down all the way to all the children again. This works for most use cases.

This however forces you to keep all your model logic (computation, transformation, validation and server-communication) in the parent view component. So you'll extract a Model out of it, in which you keep all this data and logic. All the handler methods in the parent component would then call the mutation methods in this model instead of changing the component state directly. But now you need to ensure that the component state is consistent with the model state, for which you hook into the Model's onChange callback in the component's `componentDidMount` method. This means whenever the Model mutates, the component state changes accordingly. For example, if you have a `setPageTitle` method in the Store (that's what we call our Models in Flux), the store would mutate the value when `setPageTitle` is invoked, and call its own `emitChange` method, which triggers the `onChange` callback on the component which was registered during its `componentDidMount`.

See: https://github.com/facebook/flux/blob/master/examples/flux-c.... You use EventEmitter (I prefer EventEmitter3) on the Store to make it an observable. See: https://github.com/facebook/flux/blob/master/examples/flux-c....

The first step in Flux is this: a simple View-Model separation using an EventEmitter store. Apart from the View-Model separation, this lets you share global state between multiple components. (Global state is bad for programmers, but the real world doesn't care. There are instances where you need to share the same data between multiple components and have them all update automatically)

In the ThreadStore example I linked to above, the `emitChange` method is called only through the Dispatcher at the bottom. This is the next step: we do not let any component modify the store values directly. Everything is channeled through a uni-directional dispatcher. The reason to do this is to avoid one Store talking to another Store talking to another, and each of these causing cascading state changes in the components that observe them. So instead of having multiple intertwined paths to the Store, you force the entire application to go through a single-channel dispatcher that process each Action in sequence.

The Actions are simply constants that are available globally to invoke specific behaviour across one or many stores. Each Store watches out for the Actions (a constant pushed into its mesage bus) they are interested in, and calls the appropriate method in its body. You push the Actions into the dispatcher, who is responsible for sending it to each Store.

The dispatcher uses the same EventEmitter observable pattern to bind each Store to the actions they are interested in.

Written in a hurry, but I hope it helps. The flux-chat repo (https://github.com/facebook/flux/blob/master/examples/flux-c...) is a great place to understand the entire pattern.

[+] lrichardson|11 years ago|reply
This looks to be a well-put-together implementation of flux. I recently looked over around 20 or so flux libraries to use for a react app I had started, and found only a couple that I liked. I would put this one near the top, but the one I ended up choosing has been great, but no one seems to know about it!

If anyone is interested: https://github.com/kjda/ReactFlux

It's very similar, but I think I like how Actions and subscriptions to actions are handled with it better.

[+] UberMouse|11 years ago|reply
I also decided to use ReactFlux after looking around at the flux implementations. It kind of suffers from the fact no one knows about it but it feels a lot better to use than the other implementations I looked at. I forked the library to support nested objects in the store state and I'm pretty happy with it now.

But I may switch to Marty since I'm not far along with the project and the dev tools look super helpful.

[+] agilebyte|11 years ago|reply
In Marty: since you attach a Store to a React via a Mixin that looks for a singleton, can you have a dynamic number of stores when you have a dynamic number of React components of the same type?
[+] dalacv|11 years ago|reply
This sounds like a line from a movie for a character which is the nerd/genius in the movie.

I'm not very technical, by the way.

[+] oliverwoodings|11 years ago|reply
I've contributed to this project on the state sources.

For me Marty is the best Flux implementation available right now. When reviewing other frameworks I found them to be either too restrictive by imposing non-flux limitations or the complete opposite by not implementing the flux principles well enough.

[+] STRML|11 years ago|reply
This seems very similar to Fluxxor, but solves some of the pain points.

Stores need to be extendable, and the Mixin concept (as used in React Elements) are a very useful way to do it. In this way, one can easily implement Marty's "State Sources" concept. I released a fork of Fluxxor a while ago with Mixin support [1] but it is unlikely to make it until 2.0.

The other nice parts to my eyes are:

State Mixins - sort of a hybrid between Fluxxor's StoreWatchMixin and Fluxxor-autobind [2]

Constants - This is a new one, I like it. Will have to play with it to see if it truly solves any problems; I've found that when I need to know what actions are available, I scan my actions files, simple as that.

Marty seems a little more "batteries included" than the other Flux implementations on the scene. This is a good thing, so long as it is easy to extend. Unfortunately I still don't see Mixins in Marty Stores either. In my opinion they are required for non-trivial apps.

1. https://github.com/STRML/fluxxor/tree/storeMixins

2. https://github.com/STRML/fluxxor-autobind

[+] jfhollingworth|11 years ago|reply
Author here: Marty stores do have mixins, think I forgot to add them to the docs
[+] picardo|11 years ago|reply
Nice documentation. But I don't see anything groundbreaking here. The whole rationale for Flux is to unidirectional state flow, and it does that quite well. A lot of library creators have focused on the tiny quibbles with "ease of use" around stores and constants –– "state management" is a good euphemism for this need –– but that's not really a bottleneck for me. I can manage my constants manually with Flux, and add listeners myself. If I need to indicate a dependency between to stores, I can use a waitFor in the switch/case block. It's that simple. Why should I learn a bunch of abstractions that make only an incremental difference?

My only pet peeve with Flux is that it does not let me dispatch from within an ongoing dispatch, which is not a huge deal yet because you can just defer the callback to the next event loop, but it's not allowing me to pipeline actions as I thought it should. I have to wait for the stack to empty out before I can dispatch a new action, which sucks. Also, there is no indication as to when a dispatch is over, so I am deferring dispatches by instinct –– or wrapping them in a promise might be the same thing.

[+] jfhollingworth|11 years ago|reply
Hey, author here. This is a repeat of my answer below but it's right down the bottom so might be missed:

I found that no other Flux implementation really helped with fetching data in a Fluxy way. There tended to be a lot of boiler plate code for binding stores to components. Furthermore, there was a lack of tooling for debugging. Marty helps combat these issues by introducing a number of new things:

- Fetch API for fetching data asynchronously without callbacks http://martyjs.org/guides/stores/fetching-data.html - State Mixins for binding stores to views http://martyjs.org/api/state-mixin/ - State sources for syncing state from heterogeneous sources http://martyjs.org/guides/state-sources/index.html - Chrome Developer extension (beta) for visualising the data flow and state of stores http://martyjs.org/devtools/

[+] eric_bullington|11 years ago|reply
The Flux pattern contains too much boilerplate and indirection for small or quick one-of web apps. If Marty helps to abstract some of that out, it's more than welcome to me.

Frankly, for small apps, I think you can get 90% of the benefit of Flux by using the pubsub solution of your choice and taking care about only subscribing from top-level components and passing state change down to their children. Immutable cursors are also a nice solution for small apps.

I get that for large teams working on massive code bases, and possibly even for smaller apps, Flux is a big plus. But I'm glad that projects like Marty are working on abstracting out all the boilerplate. Less code for me to worry about.

[+] thomasfl|11 years ago|reply
Even if HN readers are getting tired of all the new "Javascript librairie de jour", it's really exciting to see how new techniques are refined and improved in a fast tempo. Keep them libraries coming!
[+] joesb|11 years ago|reply
I really want to use React in my next application, but I'm lost on how to do visual things like animation (like a carousel), drag-drop, typeahead.

Do you have any suggestion on the best practice on doing those heavy DOM stuff in React?

I know I can use jQuery for that, but it feels wrong to me.

Also how do you handle integrating React with command based JS library, where your only interface to it is to call mutating functions, such that you cannot blindly rerender the whole things.

Suggestions on these two issues would help me greatly.

[+] riffraff|11 years ago|reply
In http://martyjs.org/guides/flux/index.html it says

> thanks to action types being strings you have a loosely coupled Law of Demeter architecture which is easy to grow without increasing the complexity of the code base.

I don't see the connection between cause and effect, unless this is saying "since everything is in the global state there is no coupling". Seems odd.

[+] jordanlev|11 years ago|reply
This looks really neat, and the documentation does seem nice. What I would absolutely love (and I'm sure many others as well) is a tutorial that shows how to build a very simple app from scratch... like a list of records and then click on a record to bring up an edit form (something super basic, but a tad more complex than TodoMVC). The problem I have is wrapping my head around how I put all the pieces together. Thanks!
[+] joesb|11 years ago|reply
I hope all the virtual-DOM based library comes to concensus with one based virtual-DOM library, or that it becomes part of the browser standard.

I don't mean the whole React library, I mean just "this a the DOM before, here's the change, now patch it" part.

More and more framework and UI library are going to be built around vritual-DOM concept. I don't want integration nightmare of UI-libraries working only on some React-like library.

[+] chadscira|11 years ago|reply
Does marty.js support the concept of an app context? The only implementation i know of is Yahoo's fluxible (https://github.com/yahoo/fluxible). This is mainly useful when you're trying to render your flux app on the server.
[+] jfhollingworth|11 years ago|reply
Not right now but isomorphisim is the next big feature I'm going to be working on
[+] corford|11 years ago|reply
Slightly OT but has anyone tried Marionette + React as a combo? Would be very interested to hear how smoothly (or not) it went.
[+] prottmann|11 years ago|reply
Can't see a difference between marty and fluxxor.

The examples look for me the same, except some additional functions to declare things.

[+] jfhollingworth|11 years ago|reply
Author here. I found that no other Flux implementation really helped with fetching data in a Fluxy way. There tended to be a lot of boiler plate code for binding stores to components. Furthermore, there was a lack of tooling for debugging. Marty helps combat these issues by introducing a number of new things:

- Fetch API for fetching data asynchronously without callbacks http://martyjs.org/guides/stores/fetching-data.html - State Mixins for binding stores to views http://martyjs.org/api/state-mixin/ - State sources for syncing state from heterogeneous sources http://martyjs.org/guides/state-sources/index.html - Chrome Developer extension (beta) for visualising the data flow and state of stores http://martyjs.org/devtools/