Teact
It's better than cjsx.
Build React element trees by composing functions.
You get full javascript control flow, and minimal boilerplate.
It's also quite simple, just a thin wrapper around React.createElement like JSX, making it fast and lightweight (2KB gzipped).
Usage
require 'teact' crel 'div''#root.container'-> unless propssignedIn crel 'button'onClick: handleOnClick'Sign In' creltext 'Welcome!'
Transforms into:
ReactcreateElement'div' id: rootclassName: 'container' propssignedIn ? ReactcreateElement'button' onClick: handleOnClick'Sign In' : null 'Welcome!'
Use it from your component's render method:
require 'react' require 'teact' : -> crel 'div'className: 'foo'=> crel 'div''bar'
Or in a stateless component:
= crel 'div'className: 'foo'-> crel 'div'propsbar
Nesting Components
crel
is just a thin wrapper around React.createElement,
so you can pass it components instead of crel names:
: -> crel 'div'className: 'doodad'=> crel 'span'@propschildren : => # ... : -> crel 'div'className: 'foo'=> crel DooDadonFiddled: @handleFiddle=> crel 'div'"I'm passed to DooDad.props.children"
If you need to build up a tree of elements inside a component's render method, you can
escape the element stack via the pureComponent
helper:
require 'teact' Teas = pureComponent teasmap # Without pureComponent, this would add teas to the element tree # in iteration order. With pureComponent, we just return the reversed list # of divs without adding the element tree. The caller may choose to add # the returned list. crel 'div'tea reverse : -> crel 'div'Teas@propsteas
Sugar Syntax
Teact exports bound functions for elements, giving you options for terser syntax if you're into that:
T = require 'teact' Tdiv className: 'foo'-> Ttext 'Blah!'
or the Teacup / CoffeeCup signatures:
require 'teact' div '.foo'-> text 'Blah!'
Performance
A super-basic performance test suggests that teact has no discernible impact on React rendering performance:
$ npm run benchmark > native x 5,197 ops/sec ±3.30% > teact x 5,339 ops/sec ±2.23% > Fastest is teact,native
It's also lightweight, at 5KB minified, 2KB gzipped.
How is this better than CJSX?
-
Familiar control flow with branching and loops. See examples above.
-
No transpiler to maintain.
-
No extraneous enclosing tags required:
: ->unless @propssignedIncrel 'a'href: '...''Sign in'crel 'h1''Tea Shop'Just works.
-
Half the lines of code. Those closing tags really add up.
Other folks have reached similar conclusions. They were later bit by using the React API directly when the implementation changed. A thin wrapper like Teact should minimize this risk.
Legacy
Markaby begat CoffeeKup begat CoffeeCup and DryKup which begat Teacup which begat Teact.
Contributing
$ git clone https://github.com/hurrymaplad/teact && cd teact$ npm install$ npm test