Coffee-React
Coffee-React provides a JSX-like syntax for building React components with the full awesomeness of CoffeeScript.
Included is the cjsx
executable, which is wrapper for coffee
, using coffee-react-transform and coffee-script to transform CJSX to Javascript. You can also require()
CJSX components under node for server-side rendering.
Example
neat-component.cjsx
compile it
neat-component.js
Installation
Version compatibility
3.x - React 0.13.x
2.1.x - React 0.12.1
2.x - React 0.12
1.x - React 0.11.2
0.x - React 0.11 and below
Usage
Output compiled JS to a file of the same name:
Require .cjsx files under node
As with the coffee-script
module, you need to register .cjsx
with the module loader:
Spread attributes
A recent addition to JSX (and CJSX) is 'spread attributes' which allow merging an object of props into a component, eg:
which is transformed to:
Breaking Changes in 1.0
React 0.12 will introduce changes to the way component descriptors are constructed, where the return value of React.createClass
is not a descriptor factory but simply the component class itself, and descriptors must be created manually using React.createElement
or by wrapping the component class with React.createDescriptor
.
In preparation for this, coffee-react-transform (and as a result, coffee-react) now outputs calls to React.createElement
to construct element descriptors from component classes for you, so you won't need to wrap your classes using React.createFactory
. However, for this to work you will need to be using at least React 0.11.2, which adds React.createElement
.
If you want the older style JSX output (which just desugars into function calls) then you need to use the 0.x branch, eg. 0.5.1.
Additionally, as of 1.0.0, all input files will be CJSX transformed, even if they don't have a .cjsx
extension or # @cjsx
pragma.
Related projects
coffee-react-transform, the underlying parser/transformer package.
node-cjsx:
require
CJSX files on the server (also possible with coffee-react/register).coffee-reactify: bundle CJSX files via browserify, see also cjsxify.
react-coffee-quickstart: equivalent to react-quickstart.
sprockets preprocessor: use CJSX with Rails/Sprockets
ruby coffee-react gem: transform CJSX to Coffeescript under Ruby
vim plugin for syntax highlighting
sublime text package for syntax highlighting
mimosa plugin for the mimosa build tool
gulp plugin for the gulp build tool
karma preprocessor for karma test runner