An open API service indexing awesome lists of open source software.

https://github.com/georgeosddev/xitrum-react-example


https://github.com/georgeosddev/xitrum-react-example

Last synced: 2 months ago
JSON representation

Awesome Lists containing this project

README

        

# Xitrum-React-Example

This is a React server side rendering example with Xitrum(Scala).
React is executed with [typesafehub/js-engine](https://github.com/typesafehub/js-engine) on Xitrum.
Javascript code(Saved in `src/resources/react/`) is isomorphic amd work both on client and server.

### How to run:

```sh
cd src/resources
npm install
browserify -t reactify main.js > ../../../public/bundle.js
cd ../../
sbt/sbt run
```

#Server-Side

### Action

ReactIndex.scala

```scala
@GET("react")
class ReactIndex extends Action with ReactRenderer {
override def layout = renderViewNoLayout()
def execute() {

// Properties for React component
val props = Map("msg1" -> "Hello", "msg2" -> "World")

// Create react component
// result html will be saved as reactForView(See also template jade file)
// property will saved as propsForView(See also template jade file)
renderReactView("App", props)

// Respond as xitrum way
respondView()
}
}
```

### Template

ReactIndex.jade

```jade
!!! 5
html
head
meta(content="text/html; charset=utf-8" http-equiv="content-type")
title Xitrum-React
!= currentAction.asInstanceOf[ReactIndex].propsForView
body
!

#{currentAction.asInstanceOf[ReactIndex].reactForView}

script(src={publicUrl("bundle.js")})
```

## Client-Side

main.js(will be browserify and reactify as bundle.js)

```javascript
var React = require('react'),
App = React.createFactory(require('./react/app.jsx'))

// Use exactly same property as server-side did.
// Client side javascript can took over pre-rendered HTML with React Component.
// So React will not re-render DOM, just add event listeners.
var appProps = JSON.parse(document.getElementById('App-props').getAttribute('data-json'));
React.render(App(appProps), document.getElementById("content"));
```

## Rendered HTML
```html

Xitrum-React




App



Hello

World




0




```