making Flux look as pretty as React
Cartiv provides the simplest flux experience around. It is reactive like a react component, and have similar api, so if you know react - you'll understand this immediately. No annoying boilerplate and repetitions, better separation of concerns, and above all - it is simple and fun. Just try and you will see!
Simplest usage
All you need is three files - react component
connected to (\controlled by) a store
and speaks with it via api
/**** ./api.js ****/;; /**** ./stores/TextStore.js ****/;; api name: 'text' { return title: 'Use Cartiv!' text: "It's the simplest flux you'll find"; } { this; //setState... in a store! } ; /**** ./component/SimpleComp.js ****/;;;const connect = ; @ //connect to states from store ... { apitext } //use the store's API { let text title = thisstate; //free magical state from the store return <div> <h3>title</h3> <input onChange=thisonChange value=text /> <div> ; });
About Cartiv
Simple
The goal of this library is to provide the easiest and simplest Flux experience, based on the the knowledge the react community has gained using older flux frameworks - Alt, Reflux and Redux. In Cartiv there are only Stores, Components and simple API object to communicate between them (forget about reducers, dispatchers, switch statements, global constants, action-creators etc). And yes, simplicity comes together with scalability and flexibility!
Reactive like React
Cartiv wishes to provide a fully reactive paradigm, while maintaining an API that is close as possible to React's API. React has managed to control the DOM from its components in an elegant and efficient way, using state, props and events for interactivity. Cartiv uses the same logic and language, to gain control of Components from Stores, with onSmthng
events, setState()
and other similar methods.
React component is "functional and reactive" - component(state) => view
(view is the outcome of the render function. if stateA === stateB, than the view will be also the same) .
Cartiv works the same way - store(state) => [component.state =>] view
. This FRP approach makes the implementation of time-travel and other cool things really simple.
separation of concerns
The third polar of Cartiv's architecture is a correct separation of concerns - while in most flux implementations react components are structured in a hierarchical way, with parents and children, we believe that the view should be flat!. Every Component is on the same level (data wise), and then it is free to be moved around, without being dependent on its parent. Secondly, the view should not control the state of the app, it can call an event, that will eventually do something, but how or when - it's not the view's responsibility.
architecture
Think about how a React component works:
var SimpleComp = React;
Cartiv works the same way:
- taking the inner state of Components away (effectively creating state-less components)
- connecting the component's state to a state in a Store
- the Store is unaware to compoenents, but it will listen to some events (actions)
- all actions are registered in one (or more) API object.
- Component can only call an action through the API object, and then (maybe) the store will change its own state
- auto-magically every component listening to the store will get the new state
Full Usage
$npm install cartiv --save
First - create an API :
;let API = ;;
Then, create a store:
;; let textStore = ; createStore; //if you want to use webpack's hot-module-replacement ;
Lastly, connect the component to the store:
;;; var SimpleComp = React;
If you are using react es6 classes, connect with es7 decorators */
;const connect = ; //@viewPortDecorator // make sure other decorators that returns a Component (usually those who provide props) are above `connect` (since it controls state).@ //same signature as the mixin one@//@autobind //other decorators could be anywhere ...
todoMVC example (copied from Redux repo, removed all the unnecessary parts and now it's Cartiv!)
Can you get any simpler?
(if you think you can - tell me your ideas - Cartiv is still young, and open for any thing that will help building react apps in the simplest possible way)
What's the meaning of the name?
Cartiv sort of sounds like React + reactive. In Hebrew it means popcicle - which is just simple fun and cool, like this library!
Wath's next
- Adding cartiv-history component for development, easy time travel through the history of the stores' states (state history actions history!)
- Boilerplate of Cartiv + server - adding
connect
to server from store with sockects (Meteorizing react! yay!)
acknowledgments
The internals of this library are based on reflux-core. This guarantees the stability of a mature framework with this new interface. I thank @spoike and the rest of the reflux community.
Special thanks to Yoni Levi for the logo's design
Cartiv has began as a mixin for reflux, that succeeded quite well. Later this was titled as a specification called super simple flux.
I've found out that Cartiv shares many ideas with om. great library, take a look.