The STJS bridge to Facebook's React JS gives you access to this great library using a Java syntax.
Supports both React.createClass and extends React.Component
@SyntheticType
@STJSBridge
public class HelloMessageProps extends Props{
public String name;
}
public class HelloMessage extends Component<HelloMessageProps, State> {
public String displayName = "HelloMessage";
public static Map<String, TypeChecker> propTypes = $map("name", React.PropTypes.string.isRequired);
@Override
public ReactElement<?> render() {
return React.createElement("div", null, "Hello ", this.props.name);
}
}
React.render(React.createElement(HelloMessage, new HelloMessageProps() {{ name = "John"; }}), document.getElementById("reactContainer"));This type of class is a bit more tricky to use but looks the same as the ES6 style classes, even if it is a React.createClass behind
@SyntheticType
@STJSBridge
public class HelloMessageProps extends Props{
public String name;
}
@IsReactClass
public class HelloMessage extends ReactClass<HelloMessageProps, State> {
public String displayName = "HelloMessage";
public static Map<String, TypeChecker> propTypes = $map("name", React.PropTypes.string.isRequired);
@Override
public ReactElement<?> render() {
return React.createElement("div", null, "Hello ", this.props.name);
}
}
React.render(React.createElement(HelloMessage, new HelloMessageProps() {{ name = "John"; }}), document.getElementById("reactContainer"));Differences to notice :
- the component extends
ReactClassinstead ofComponent - there is a
@IsReactClassannotation propTypesis declared asstatic
- Add the maven dependency to your project.
<dependency>
<groupId>org.st-js.bridge</groupId>
<artifactId>react</artifactId>
<version>0.13.3.bv1</version>
</dependency>- Enjoy, you're good to go if you don't wish to use
React.createClass; read-on if you do.
- Add the following snippet to your stjs-maven-plugin in your project
<annotations>
<annotation>IsReactClass</annotation>
<annotation>IsReactMixin</annotation>
</annotations>- Include
stjs-react.jsin your page afterstjs.jsbut before your generated classes