GithubHelp home page GithubHelp logo

eddyson-de / tapestry-react Goto Github PK

View Code? Open in Web Editor NEW
17.0 8.0 5.0 1.32 MB

Integration of Apache Tapestry and React

License: Apache License 2.0

Java 61.32% JavaScript 2.51% Groovy 28.98% CoffeeScript 6.98% Less 0.21%
tapestry react jsx

tapestry-react's Introduction

tapestry-react

status 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 templates with Tapestry.

Usage

build.gradle:

repositories {
  maven { url "https://jitpack.io" }
}

dependencies {
  implementation 'com.github.eddyson-de:tapestry-react:0.36.0'
}

That's it, now you can import modules written in JSX. Just give them the .jsx(m) extension and they will be compiled to JavaScript automatically.

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

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

Components

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

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

define(['react'], function(React) {
  return React.createClass({
    render: function() {
      return <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. 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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.