Skip to content

eduardbaitinger/tapestry-react

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tapestry-react

Build Status Greenkeeper badge Join the chat at https://gitter.im/eddyson-de/tapestry-react

Use React (http://facebook.github.io/react/index.html) together with Tapestry (http://tapestry.apache.org/).

This library provides basic integration for using JSX and CJSX templates with Tapestry.

Usage

build.gradle:

repositories {
  jcenter()
}

dependencies {
  runtime 'de.eddyson:tapestry-react:0.30.1'
}

That's it, now you can import modules written in (C)JSX. Just give them the .(c)jsx extension and they will be compiled to JavaScript automatically.
Note: CJSX transformation may not work with React 0.14 and/or React 15 features.

/META-INF/modules/app/react-test.cjsx:

define ['t5/core/dom', 'react', 'react-dom'], (dom, React, ReactDOM)->
  HelloMessage = React.createClass
    render: -> <div>Hello {this.props.name}</div>
  mountNode = (dom 'example').element
  ReactDOM.render <HelloMessage name="John" />, mountNode
  return

Components

You can also use the ReactComponent component to keep the components' code separate from the page code:

/META-INF/modules/app/react/HelloMessage.cjsx:

define ['react'], (React)->
  React.createClass
    render: -> <div>Hello {this.props.name}</div>

/org/example/app/pages/ReactDemo.tml:

<html title="React Demo Index"
	xmlns:t="http://tapestry.apache.org/schema/tapestry_5_4.xsd"
	xmlns:r="tapestry-library:react">
	<div class="row">
		<div class="col-md-10 col-md-offset-1 col-lg-8 col-lg-offset-2">
			<r:reactcomponent module="app/react/HelloMessage" name="John"/>
		</div>
	</div>
</html>

ECMAScript 6 modules => AMD

If you want to write your classes as ES6 rather than AMD moudules, just use the .jsxm file extension to switch the transpiler to AMD output.

/META-INF/modules/app/react/HelloMessage.jsxm:

import React from 'react'

export default class HelloMessage extends React.Component {

  constructor(props){
    super(props);  
  }
  
  render(){
    return (
      <div>Hello {this.props.name}!</div>
    )
  }
}

Development code

If you want code to be executed only in development mode but not in production, you can use the __DEV__ pseudo variable:

if (__DEV__) {
  MyComponent.propTypes = {
    ...
  }
}

This will be compiled to if (true) or if (false) depending on the value of the tapestry.production-mode symbol.

Standalone compiler

If you want to compile code outside of a Tapestry application (e.g. in your Gradle build), you can use the de.eddyson.tapestry.react.StandaloneCompiler and de.eddyson.tapestry.react.readers.CompilingBabelReader classes.

Demo?

Unfortunately, there is no live demo available, but the test application can be examined by running ./gradlew runTestApp and pointing your browser to http://localhost:9040/.

Notes

Speeding things up in production

Compiling templates can take some time, especially when using CJSX. Combined with minification, this can quickly lead to Require.js timeouts in production.
To speed things up, you can have the files pre-compiled and minified upon registry startup using https://github.com/eddyson-de/tapestry-minification-cache-warming.

Calling server-side code

You will probably end up having a lot of React components that do not have an associated page class. If this is the case and you find yourself wanting a proper REST API rather than component- or page-level event handlers, have a look at https://github.com/tynamo/tapestry-resteasy.

About

Integration of Apache Tapestry and React

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 41.9%
  • Java 37.7%
  • Groovy 16.3%
  • CoffeeScript 4.0%
  • CSS 0.1%