SlideShare a Scribd company logo
1 of 66
Download to read offline
Using React with Grails 3
Zachary Klein
Software Engineer, OCI
Slides/Sample Code:
http://bit.ly/2fD91Qf
About Me
Grails & web developer for 5 years
Joined OCI Grails team in 2015
Using React since 2015
Author of the React profile/s for Grails 3
@zacharyaklein
On the Grails Team at @ObjectComputing.
Christ-follower, husband (of Elizabeth), dad (of
John & Timmy) programmer, guitarist. Needing
to be more like Jesus.
About you
Where we’re going today
• What is React?
• Brief overview of React and friends (npm, webpack,
babel)
• Only a primer - there’s lots more!
• How do I use React with Grails?
• Asset Pipeline plugin/s
• React profile - monolith
• React profile 2.x - separate client & server
• Isomorphic React - server-side rendering w/Nashorn
• Resources & Q/A
What is React?
A Javascript library for building 

user interfaces

Key Features:

• Virtual DOM

• JSX

• Components

• Single Page Apps

• Functional emphasis

• Beyond the browser
“React is a JavaScript
library,and so it assumes you have a
basic understanding of the JavaScript
language.”
What is React?
import React from ‘react’
import ReactDOM from ‘react-dom’
ReactDOM.render(
<h1>Hello, world!</h1>,
document.getElementById('root')
);
What is React?
import React from ‘react’
import ReactDOM from ‘react-dom’
ReactDOM.render(
<h1>Hello, world!</h1>,
document.getElementById('root')
);
What is React?
import React from ‘react’
import ReactDOM from ‘react-dom’
class Greeting extends React.Component {
render() {
return <h1>Hello, {this.props.name}!</h1>;
}
}
ReactDOM.render(
<Greeting name=‘G3 Summit’ />,
document.getElementById('root')
);
What is React?
var React = require(‘react’);
var ReactDOM = require(‘react-dom’);
var Greeting = React.createClass({
render: function() {
return <h1>Hello, {this.props.name}</h1>;
}
});
ReactDOM.render(
<Greeting name=‘G3 Summit’ />,
document.getElementById('root')
);
What is React?
import React from ‘react’
import ReactDOM from ‘react-dom’
class Greeting extends React.Component {
render() {
return <h1>Hello, {this.props.name}!</h1>;
}
}
ReactDOM.render(
<Greeting name=‘G3 Summit’ />,
document.getElementById('root')
);
What is React?
var React = require(‘react’);
var ReactDOM = require(‘react-dom’);
var Greeting = React.createClass({
render: function() {
return React.createElement(
'h1', null, `Hello, ${this.props.name}!`
);
}
});
ReactDOM.render(
React.createElement(Greeting, {name: ‘G3 Summit'}, null),
document.getElementById('root')
);
What is React?
var React = require(‘react’);
var ReactDOM = require(‘react-dom’);
var Greeting = React.createClass({
render: function() {
return React.createElement(
'h1', null, `Hello, ${this.props.name}!`
);
}
});
ReactDOM.render(
React.createElement(Greeting, {name: ‘G3 Summit'}, null),
document.getElementById('root')
);
name bodyprops
name bodyprops
What is React?
import React from ‘react’
import ReactDOM from ‘react-dom’
class Greeting extends React.Component {
render() {
return <h1>Hello, {this.props.name}!</h1>;
}
}
ReactDOM.render(
<Greeting name=‘G3 Summit’ />,
document.getElementById('root')
);
name body
propsname
What is React?
class Greeting extends React.Component {
render() {
return(
React.createElement('div', {},
React.createElement('h1', {}, “Greetings, ${this.props.name}"),
React.createElement('ul', {},
React.createElement('li', {},
React.createElement('a', {href: ‘edit'},
‘Edit this greeting’)
),
React.createElement('li', {},
React.createElement('a', {href: 'reset'},
‘Reset this greeting')
)
)
);
}
}
ReactDOM.render(
React.createElement(Greeting, {name: ‘G3 Summit'}, null),
document.getElementById('root')
);
What is React?
class Greeting extends React.Component {
render() {
return (<div>
<h1>Hello, {this.props.name}!</h1>
<ul>
<li><a href=‘edit’>Edit this greeting</a></li>
<li><a href=‘reset’>Reset this greeting</a></li>
</ul>
</div>);
}
}
ReactDOM.render(
<Greeting name=‘G3 Summit’ />,
document.getElementById('root')
);
What is React?
“React is only the view layer.
We're only in one concern. React only knows how to render markup. It
doesn't know where your data came from, how it's stored, or how to
manipulate it. What concerns are being violated?”
Andrew Ray, via http://blog.andrewray.me/youre-missing-the-point-of-jsx/
Component State
class Greeting extends React.Component {
constructor() {
super();
this.state = {
greetings: ['Hello', 'Salutations', 'Ho there’]
}
}
render() {
const greetings = this.state.greetings;
const randomGreeting = greetings[Math.floor(Math.random() * greetings.length];
return(
<h1>{randomGreeting}, {this.props.name}</h1>
);
}
}
ReactDOM.render(
React.createElement(<Greeting name=‘G3 Summit’ />, document.getElementById('root')
);
Component State
<Greeting 📊 />
<App 📊 />
<Greeting data={⬆ }/>
Redux.createStore( 📊)
<App ⬆ />
<Greeting ⬆ />
<App 📊 />
Each component? Top-level component? External store?
Where should state go? 📊
Component State
<App 📊 />
<Greeting data={⬆ }/>
Recommendation:
Store all state in top-level component
Pass state down to children
components via props
📊
What is React?
Other features:

• Lifecycle methods

• Event-handling

• State management

• PropType checking

• DOM access via refs
• SPA or partials

“React is, in our
opinion,the premier way to build
big, fast Web apps with JavaScript. It
has scaled very well for us at Facebook
and Instagram.”
What is React?
https://www.toptal.com/react/navigating-the-react-ecosystem
React Bootstrap
React is a small, focused library by design, but there’s plenty of options for augmentation.

fetch axios
What is React?
React can be used standalone, but is more frequently used with node, npm & friends.
What is React?
“Node.js is an open-source, cross-platform JavaScipt runtime environment.” (Wikipedia)
What is React?
“npm is the default package manager for… Node.js.” (Wikipedia)
What is React?
The 6th edition of ECMAScript, officially titled ECMAScript 2015.
What is React?
Babel - a Javascript “transpiler”
What is React?
Webpack is an open-source Javascript module bundler. (official website)
Why use React with Grails?
Why use React with Grails?
• Rest support
• rest-api profile
• JSON views
• Spring Websockets
• Gradle
+
How do I use React with Grails?
• Most React community resources & documentation
assume a node/Javascript-based environment.
• Typical project structure is not immediately
transferable to a Grails project.
• Use of npm is ubiquitous (but not required) and brings
both advantages & challenges.
• Potential tension between front-end and back-end
developer workflows
• Wild west - the trail is still being blazed!
How do I use React with Grails?
What build system
would you like to see?
Other
6%
grails-app/assets
40%
src/main/webapp
27%
Separate app
27%
Where should the client
app live?
Other
8%
Asset Pipeline
48%
Webpack
34%
SystemJS
10%
React & the Asset Pipeline
React & the Asset Pipeline
• Front-end asset management/processing in Grails is
typically handled via the Asset Pipeline.
• Recently-released plugins now support React with AP.
• React/JSX files in grails-app/assets/javascripts.
• Take advantage of other front-end Grails/Gradle
plugins, such as the excellent client-dependencies
plugin (https://github.com/craigburke/client-
dependencies-gradle)
• Performant, extensible, familiar to Grails developers.
React & the Asset Pipeline
• Babel Asset-Pipeline Plugin
• Makes use of the Babel transpiler to transform ES6
code to ES5
• Supports Webpack bundler, hot-reloading
• Promising, but tricky to configure
compile(‘:babel-asset-pipeline:2.1.0')
React & the Asset Pipeline
• JSX Asset-Pipeline Plugin
• Natively parses React/JSX code w/o Babel
• Follows Asset Pipeline conventions
• Works well with client-dependencies plugin
• Documentation is lacking
• Excellent option for teams experienced/invested in AP

assets "com.bertramlabs.plugins:jsx-asset-pipeline:2.12.0"
React & the Asset Pipeline
DEMO
React & the Asset Pipeline
• “Asset pipeline” seems to be out of favor among
front-end developers (perhaps unfairly).
• May be difficult to follow along with community
documentation/resources.
• AP support for new asset processing tools tends to lag
behind Node-based tooling.
• Not compatible with popular front-end toolchains.
• Grails-centric - may be confusing/unfamiliar to a
dedicated front-end team.
Webpack
• Webpack is a Module bundler.
• Typically installed via npm, run via scripts in package.json
• Designed for Single Page Apps
• Supports a huge variety of asset types via configurable
“loaders” (processors)
• Supports code-splitting/chunking
• Supports hashed/version assets for cache control
• Outputs a single bundle (by default), containing all
required Javascript/CSS to render the target (i.e, your
React app)
import ‘text-label’
_react2.default.createElement(
_reactBootstrap.Modal.Footer,
null,
_react2.default.createElement(
'span',
{ className: 'copyright' },
'Built with React 15.3.2, webpack
1.13.1, Grails 3'
),
_react2.default.createElement(
_reactBootstrap.Button,
{ onClick: this.toggleModal },
'Close'
)
hello.js
bundle.js
import ‘lodash’
text-label.js
//from node_modules
loadash.js
import ‘hello’
app.js
function hello() {
return <div>Hello world!</div>;
}
"use strict";
function hello() {
return React.createElement(
"div",
null,
"Hello world!"
);
}
hello.js
bundle.js
- loader
Asset Pipeline vs Webpack
Asset Pipeline vs Webpack
Webpack & Grails +
src/main/webapp
Grails App
React App (UI)
<RESTAPI>
Webpack & Grails +
Grails App
React App (UI)
<RESTAPI>
Webpack & Grails
• Compromise:
• Configure Webpack to output bundle/s into grails-app/assets/
javascripts
• Keep React source code in a separate directory tree (e.g, src/
main/webapp)
• Rely on Webpack for processing our React/ES6 code
• Continue to use AP for non-React assets (jQuery, bootstrap,
scaffolded pages, etc), as well as to serve the webpack bundle
to the browser.
• Use Gradle to automate running webpack via npm scripts
• All this can be tricky to configure - if only there was an easier
way…
+
React Profile for Grails 3
grails create-app myReactApp —profile=org.grails.profiles:react:1.0.2
React 1.x Profile generates a Grails project with the following:
• React, ReactDOM etc., installed via npm
• Webpack configured to process React code and output to
grails-app/assets/javascripts
• gradle-node plugin installed, custom tasks to run webpack
on app startup/packaging
• Sample React code & unit tests
React Profile for Grails 3
src/main/webapp
Grails App
React App (UI)
React Profile for Grails 3
$ ls -l
grails-app/
- assets/
- - javascripts/
- - - bundle.js
node_modules/
package.json
src/
- main/
- - webapp/
- - - app/
- - - - about.js
- - test/
- - - js/
- - - - about.spec.js
webpack.config.js
npm project file
webpack configuration file
React source code
webpack bundle
Unit test
React Profile for Grails 3
DEMO
React Profile for Grails 3
• The React 1.x profile simplifies the setup process for
using React & Webpack with Grails
• Designed for monolithic applications
• Gradle-node plugin ties the two “worlds” together
• Single development/test/deployment path
• Gradle wrapper allows front-end devs to run the Grails
app w/o installing Grails
• Gradle-node tasks allow Grails devs to run webpack
w/o installing npm
./gradlew bootRun
./gradlew webpack
Separate Client & Server
• Microservice-friendly
• Deploy/update front-end and back-end separately
• Take advantage of Grails’ RESTful features
• Domain resources
• JSON Views
• Gradle multi-project build for client & server apps
• Requires fully standalone React app, including:
• webpack, babel & dev-server
• CORS configuration
Separate Client & Server
Grails App (server)
React App (client)
<RESTAPI>
create-react-app
“Create React App is a new officially supported way to
create single-page React applications. It offers a modern
build setup with no configuration.”
https://facebook.github.io/react/blog/2016/07/22/create-apps-with-no-configuration.html
• Generates fully standalone React app
• Provides working webpack & Babel config
• Provides scripts for starting app, building public bundle,
running tests
• Simplified development & an easy “exit strategy”
React Profile for Grails 3
grails create-app myReactApp —profile=react
React 2.x Profile generates a multi-project Gradle build:
• React app (generated via create-react-app) as client
project
• Grails 3 app (rest-api profile) as server project
• Gradle-node tasks defined within client project to run npm
scripts (start, build, test, & eject)
• Grails index page built with react-bootstrap, with app data
populated via REST call
React Profile for Grails 3
Grails App
React App
<RESTAPI>
client server
React Profile for Grails 3
$ ls -l
client/
- build.gradle
- node_modules/
- package.json
- public/
- src/
- - App.js
- - App.test.js
server/
settings.gradle
npm project file
Grails 3 app
React source code
React app
Unit test
Gradle project file
React Profile for Grails 3
DEMO
Isomorphic React
• Isomorphic - same code on client & server (aka “universal”)
• React can be rendered server-side
• Can improve initial page load time
• Can improve SEO performance
• Java 8 introduced a new JavaScript engine, Nashorn
Isomorphic React
NashornScriptEngine nashornScriptEngine =
new ScriptEngineManager().getEngineByName("nashorn")
nashornScriptEngine.eval(readResource(“myscript.js”))
nashornScriptEngine.invokeFunction('myFunction',
“${new JsonBuilder([arg:‘some arg’])}”)
function myFunction(data) {
render(“Here’s some data we got from the server: “ + data.arg);
}
Isomorphic React
DEMO
Isomorphic React
• Tightly couples React source code with Grails app
• Adds additional complexity
• Requires polyfill to work with React
• Nashorn's CSS/LESS support is very poor
• Perhaps useful for React scaffolding?
• Performance?
Resources
React Ecosystem
• https://www.toptal.com/react/navigating-the-react-ecosystem
JSX
• http://jamesknelson.com/learned-stop-worrying-love-jsx/
• http://blog.andrewray.me/youre-missing-the-point-of-jsx/
React Architecture
• https://facebook.github.io/react/docs/thinking-in-react.html
• https://discuss.reactjs.org/t/best-practices-for-extending-
subclassing-components/1820
Resources
Grails Plugins & Profiles
• https://grails.org/plugin/babel-asset-pipeline
• https://github.com/bertramdev/asset-pipeline/tree/master/jsx-asset-
pipeline
• https://github.com/ZacharyKlein/grails-isomorphic
• https://github.com/grails-profiles/webpack
• https://github.com/grails-profiles/react
Using React & Grails
• http://grailsblog.objectcomputing.com/posts/2016/05/28/using-react-
with-grails.html
• http://grailsblog.objectcomputing.com/posts/2016/11/14/
introducing-the-react-profile-for-grails.html
• http://guides.grails.org/using-the-react-profile/guide/index.html
Thank you!
twitter: @zacharyaklein
mailto: kleinz@ociweb.com

More Related Content

What's hot

Angular 2 Migration - JHipster Meetup 6
Angular 2 Migration - JHipster Meetup 6Angular 2 Migration - JHipster Meetup 6
Angular 2 Migration - JHipster Meetup 6William Marques
 
Grails Plugin Best Practices
Grails Plugin Best PracticesGrails Plugin Best Practices
Grails Plugin Best PracticesBurt Beckwith
 
Java Microservices with Spring Boot and Spring Cloud - Denver JUG 2019
Java Microservices with Spring Boot and Spring Cloud - Denver JUG 2019Java Microservices with Spring Boot and Spring Cloud - Denver JUG 2019
Java Microservices with Spring Boot and Spring Cloud - Denver JUG 2019Matt Raible
 
A Closer Look At React Native
A Closer Look At React NativeA Closer Look At React Native
A Closer Look At React NativeIan Wang
 
Building Grails Plugins - Tips And Tricks
Building Grails Plugins - Tips And TricksBuilding Grails Plugins - Tips And Tricks
Building Grails Plugins - Tips And TricksMike Hugo
 
My "Perfect" Toolchain Setup for Grails Projects
My "Perfect" Toolchain Setup for Grails ProjectsMy "Perfect" Toolchain Setup for Grails Projects
My "Perfect" Toolchain Setup for Grails ProjectsGR8Conf
 
Developing Modern Java Web Applications with Java EE 7 and AngularJS
Developing Modern Java Web Applications with Java EE 7 and AngularJSDeveloping Modern Java Web Applications with Java EE 7 and AngularJS
Developing Modern Java Web Applications with Java EE 7 and AngularJSShekhar Gulati
 
Starting with Reactjs
Starting with ReactjsStarting with Reactjs
Starting with ReactjsThinh VoXuan
 
手把手教你如何串接 Log 到各種網路服務
手把手教你如何串接 Log 到各種網路服務手把手教你如何串接 Log 到各種網路服務
手把手教你如何串接 Log 到各種網路服務Mu Chun Wang
 
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav DukhinFwdays
 
Spring IO '15 - Developing microservices, Spring Boot or Grails?
Spring IO '15 - Developing microservices, Spring Boot or Grails?Spring IO '15 - Developing microservices, Spring Boot or Grails?
Spring IO '15 - Developing microservices, Spring Boot or Grails?Fátima Casaú Pérez
 
Using JHipster for generating Angular/Spring Boot apps
Using JHipster for generating Angular/Spring Boot appsUsing JHipster for generating Angular/Spring Boot apps
Using JHipster for generating Angular/Spring Boot appsYakov Fain
 
High Performance Microservices with Ratpack and Spring Boot
High Performance Microservices with Ratpack and Spring BootHigh Performance Microservices with Ratpack and Spring Boot
High Performance Microservices with Ratpack and Spring BootDaniel Woods
 
From Spring Boot 2.2 to Spring Boot 2.3 #jsug
From Spring Boot 2.2 to Spring Boot 2.3 #jsugFrom Spring Boot 2.2 to Spring Boot 2.3 #jsug
From Spring Boot 2.2 to Spring Boot 2.3 #jsugToshiaki Maki
 
GR8Conf 2011: Grails, how to plug in
GR8Conf 2011: Grails, how to plug inGR8Conf 2011: Grails, how to plug in
GR8Conf 2011: Grails, how to plug inGR8Conf
 
Java REST API Framework Comparison - UberConf 2021
Java REST API Framework Comparison - UberConf 2021Java REST API Framework Comparison - UberConf 2021
Java REST API Framework Comparison - UberConf 2021Matt Raible
 
Play Framework workshop: full stack java web app
Play Framework workshop: full stack java web appPlay Framework workshop: full stack java web app
Play Framework workshop: full stack java web appAndrew Skiba
 
Magento NodeJS Microservices — Yegor Shytikov | Magento Meetup Online #11
Magento NodeJS Microservices — Yegor Shytikov | Magento Meetup Online #11Magento NodeJS Microservices — Yegor Shytikov | Magento Meetup Online #11
Magento NodeJS Microservices — Yegor Shytikov | Magento Meetup Online #11Magecom UK Limited
 

What's hot (20)

Angular 2 Migration - JHipster Meetup 6
Angular 2 Migration - JHipster Meetup 6Angular 2 Migration - JHipster Meetup 6
Angular 2 Migration - JHipster Meetup 6
 
Grails Plugin Best Practices
Grails Plugin Best PracticesGrails Plugin Best Practices
Grails Plugin Best Practices
 
Introducing spring
Introducing springIntroducing spring
Introducing spring
 
Java Microservices with Spring Boot and Spring Cloud - Denver JUG 2019
Java Microservices with Spring Boot and Spring Cloud - Denver JUG 2019Java Microservices with Spring Boot and Spring Cloud - Denver JUG 2019
Java Microservices with Spring Boot and Spring Cloud - Denver JUG 2019
 
A Closer Look At React Native
A Closer Look At React NativeA Closer Look At React Native
A Closer Look At React Native
 
Building Grails Plugins - Tips And Tricks
Building Grails Plugins - Tips And TricksBuilding Grails Plugins - Tips And Tricks
Building Grails Plugins - Tips And Tricks
 
My "Perfect" Toolchain Setup for Grails Projects
My "Perfect" Toolchain Setup for Grails ProjectsMy "Perfect" Toolchain Setup for Grails Projects
My "Perfect" Toolchain Setup for Grails Projects
 
Developing Modern Java Web Applications with Java EE 7 and AngularJS
Developing Modern Java Web Applications with Java EE 7 and AngularJSDeveloping Modern Java Web Applications with Java EE 7 and AngularJS
Developing Modern Java Web Applications with Java EE 7 and AngularJS
 
Micronaut Launchpad
Micronaut LaunchpadMicronaut Launchpad
Micronaut Launchpad
 
Starting with Reactjs
Starting with ReactjsStarting with Reactjs
Starting with Reactjs
 
手把手教你如何串接 Log 到各種網路服務
手把手教你如何串接 Log 到各種網路服務手把手教你如何串接 Log 到各種網路服務
手把手教你如何串接 Log 到各種網路服務
 
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
 
Spring IO '15 - Developing microservices, Spring Boot or Grails?
Spring IO '15 - Developing microservices, Spring Boot or Grails?Spring IO '15 - Developing microservices, Spring Boot or Grails?
Spring IO '15 - Developing microservices, Spring Boot or Grails?
 
Using JHipster for generating Angular/Spring Boot apps
Using JHipster for generating Angular/Spring Boot appsUsing JHipster for generating Angular/Spring Boot apps
Using JHipster for generating Angular/Spring Boot apps
 
High Performance Microservices with Ratpack and Spring Boot
High Performance Microservices with Ratpack and Spring BootHigh Performance Microservices with Ratpack and Spring Boot
High Performance Microservices with Ratpack and Spring Boot
 
From Spring Boot 2.2 to Spring Boot 2.3 #jsug
From Spring Boot 2.2 to Spring Boot 2.3 #jsugFrom Spring Boot 2.2 to Spring Boot 2.3 #jsug
From Spring Boot 2.2 to Spring Boot 2.3 #jsug
 
GR8Conf 2011: Grails, how to plug in
GR8Conf 2011: Grails, how to plug inGR8Conf 2011: Grails, how to plug in
GR8Conf 2011: Grails, how to plug in
 
Java REST API Framework Comparison - UberConf 2021
Java REST API Framework Comparison - UberConf 2021Java REST API Framework Comparison - UberConf 2021
Java REST API Framework Comparison - UberConf 2021
 
Play Framework workshop: full stack java web app
Play Framework workshop: full stack java web appPlay Framework workshop: full stack java web app
Play Framework workshop: full stack java web app
 
Magento NodeJS Microservices — Yegor Shytikov | Magento Meetup Online #11
Magento NodeJS Microservices — Yegor Shytikov | Magento Meetup Online #11Magento NodeJS Microservices — Yegor Shytikov | Magento Meetup Online #11
Magento NodeJS Microservices — Yegor Shytikov | Magento Meetup Online #11
 

Similar to Using React with Grails 3

Introduction to React native
Introduction to React nativeIntroduction to React native
Introduction to React nativeDhaval Barot
 
Integrating React.js Into a PHP Application
Integrating React.js Into a PHP ApplicationIntegrating React.js Into a PHP Application
Integrating React.js Into a PHP ApplicationAndrew Rota
 
MidwestJS 2014 Reconciling ReactJS as a View Layer Replacement
MidwestJS 2014 Reconciling ReactJS as a View Layer ReplacementMidwestJS 2014 Reconciling ReactJS as a View Layer Replacement
MidwestJS 2014 Reconciling ReactJS as a View Layer ReplacementZach Lendon
 
Reconciling ReactJS as a View Layer Replacement (MidwestJS 2014)
Reconciling ReactJS as a View Layer Replacement (MidwestJS 2014)Reconciling ReactJS as a View Layer Replacement (MidwestJS 2014)
Reconciling ReactJS as a View Layer Replacement (MidwestJS 2014)Zach Lendon
 
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...Codemotion
 
Intro to Ratpack (CDJDN 2015-01-22)
Intro to Ratpack (CDJDN 2015-01-22)Intro to Ratpack (CDJDN 2015-01-22)
Intro to Ratpack (CDJDN 2015-01-22)David Carr
 
Cannibalising The Google App Engine
Cannibalising The  Google  App  EngineCannibalising The  Google  App  Engine
Cannibalising The Google App Enginecatherinewall
 
node.js 실무 - node js in practice by Jesang Yoon
node.js 실무 - node js in practice by Jesang Yoonnode.js 실무 - node js in practice by Jesang Yoon
node.js 실무 - node js in practice by Jesang YoonJesang Yoon
 
Google App Engine Java, Groovy and Gaelyk
Google App Engine Java, Groovy and GaelykGoogle App Engine Java, Groovy and Gaelyk
Google App Engine Java, Groovy and GaelykGuillaume Laforge
 
ReactJS vs AngularJS - Head to Head comparison
ReactJS vs AngularJS - Head to Head comparisonReactJS vs AngularJS - Head to Head comparison
ReactJS vs AngularJS - Head to Head comparison500Tech
 
Gwt and rpc use 2007 1
Gwt and rpc use 2007 1Gwt and rpc use 2007 1
Gwt and rpc use 2007 1Sam Muhanguzi
 
Practical Use of MongoDB for Node.js
Practical Use of MongoDB for Node.jsPractical Use of MongoDB for Node.js
Practical Use of MongoDB for Node.jsasync_io
 
Advanced Web Technology.pptx
Advanced Web Technology.pptxAdvanced Web Technology.pptx
Advanced Web Technology.pptxssuser35fdf2
 
React - Start learning today
React - Start learning today React - Start learning today
React - Start learning today Nitin Tyagi
 
Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]GDSC UofT Mississauga
 
Play Support in Cloud Foundry
Play Support in Cloud FoundryPlay Support in Cloud Foundry
Play Support in Cloud Foundryrajdeep
 

Similar to Using React with Grails 3 (20)

Introduction to React native
Introduction to React nativeIntroduction to React native
Introduction to React native
 
Integrating React.js Into a PHP Application
Integrating React.js Into a PHP ApplicationIntegrating React.js Into a PHP Application
Integrating React.js Into a PHP Application
 
MidwestJS 2014 Reconciling ReactJS as a View Layer Replacement
MidwestJS 2014 Reconciling ReactJS as a View Layer ReplacementMidwestJS 2014 Reconciling ReactJS as a View Layer Replacement
MidwestJS 2014 Reconciling ReactJS as a View Layer Replacement
 
Reconciling ReactJS as a View Layer Replacement (MidwestJS 2014)
Reconciling ReactJS as a View Layer Replacement (MidwestJS 2014)Reconciling ReactJS as a View Layer Replacement (MidwestJS 2014)
Reconciling ReactJS as a View Layer Replacement (MidwestJS 2014)
 
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...
 
Intro to Ratpack (CDJDN 2015-01-22)
Intro to Ratpack (CDJDN 2015-01-22)Intro to Ratpack (CDJDN 2015-01-22)
Intro to Ratpack (CDJDN 2015-01-22)
 
Cannibalising The Google App Engine
Cannibalising The  Google  App  EngineCannibalising The  Google  App  Engine
Cannibalising The Google App Engine
 
Gradle
GradleGradle
Gradle
 
node.js 실무 - node js in practice by Jesang Yoon
node.js 실무 - node js in practice by Jesang Yoonnode.js 실무 - node js in practice by Jesang Yoon
node.js 실무 - node js in practice by Jesang Yoon
 
Google App Engine Java, Groovy and Gaelyk
Google App Engine Java, Groovy and GaelykGoogle App Engine Java, Groovy and Gaelyk
Google App Engine Java, Groovy and Gaelyk
 
ReactJS vs AngularJS - Head to Head comparison
ReactJS vs AngularJS - Head to Head comparisonReactJS vs AngularJS - Head to Head comparison
ReactJS vs AngularJS - Head to Head comparison
 
Gwt and rpc use 2007 1
Gwt and rpc use 2007 1Gwt and rpc use 2007 1
Gwt and rpc use 2007 1
 
Practical Use of MongoDB for Node.js
Practical Use of MongoDB for Node.jsPractical Use of MongoDB for Node.js
Practical Use of MongoDB for Node.js
 
Treinamento frontend
Treinamento frontendTreinamento frontend
Treinamento frontend
 
Advanced Web Technology.pptx
Advanced Web Technology.pptxAdvanced Web Technology.pptx
Advanced Web Technology.pptx
 
React - Start learning today
React - Start learning today React - Start learning today
React - Start learning today
 
Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]Full Stack React Workshop [CSSC x GDSC]
Full Stack React Workshop [CSSC x GDSC]
 
Graphql usage
Graphql usageGraphql usage
Graphql usage
 
Play Support in Cloud Foundry
Play Support in Cloud FoundryPlay Support in Cloud Foundry
Play Support in Cloud Foundry
 
Node.js an Exectutive View
Node.js an Exectutive ViewNode.js an Exectutive View
Node.js an Exectutive View
 

More from Zachary Klein

Native Cloud-Native: Building Agile Microservices with the Micronaut Framework
Native Cloud-Native: Building Agile Microservices with the Micronaut FrameworkNative Cloud-Native: Building Agile Microservices with the Micronaut Framework
Native Cloud-Native: Building Agile Microservices with the Micronaut FrameworkZachary Klein
 
Groovy-Powered Microservices with Micronaut
Groovy-Powered Microservices with MicronautGroovy-Powered Microservices with Micronaut
Groovy-Powered Microservices with MicronautZachary Klein
 
Micronaut: Changing the Micro Future
Micronaut: Changing the Micro FutureMicronaut: Changing the Micro Future
Micronaut: Changing the Micro FutureZachary Klein
 
Grails 4: Upgrade your Game!
Grails 4: Upgrade your Game!Grails 4: Upgrade your Game!
Grails 4: Upgrade your Game!Zachary Klein
 
Getting Groovy with JHipster and Micronaut
Getting Groovy with JHipster and MicronautGetting Groovy with JHipster and Micronaut
Getting Groovy with JHipster and MicronautZachary Klein
 
Groovy for Java Devs
Groovy for Java DevsGroovy for Java Devs
Groovy for Java DevsZachary Klein
 
Grails Launchpad - From Ground Zero to Orbit
Grails Launchpad - From Ground Zero to OrbitGrails Launchpad - From Ground Zero to Orbit
Grails Launchpad - From Ground Zero to OrbitZachary Klein
 
Room with a Vue - Introduction to Vue.js
Room with a Vue - Introduction to Vue.jsRoom with a Vue - Introduction to Vue.js
Room with a Vue - Introduction to Vue.jsZachary Klein
 
Shields Up! Securing React Apps
Shields Up! Securing React AppsShields Up! Securing React Apps
Shields Up! Securing React AppsZachary Klein
 

More from Zachary Klein (9)

Native Cloud-Native: Building Agile Microservices with the Micronaut Framework
Native Cloud-Native: Building Agile Microservices with the Micronaut FrameworkNative Cloud-Native: Building Agile Microservices with the Micronaut Framework
Native Cloud-Native: Building Agile Microservices with the Micronaut Framework
 
Groovy-Powered Microservices with Micronaut
Groovy-Powered Microservices with MicronautGroovy-Powered Microservices with Micronaut
Groovy-Powered Microservices with Micronaut
 
Micronaut: Changing the Micro Future
Micronaut: Changing the Micro FutureMicronaut: Changing the Micro Future
Micronaut: Changing the Micro Future
 
Grails 4: Upgrade your Game!
Grails 4: Upgrade your Game!Grails 4: Upgrade your Game!
Grails 4: Upgrade your Game!
 
Getting Groovy with JHipster and Micronaut
Getting Groovy with JHipster and MicronautGetting Groovy with JHipster and Micronaut
Getting Groovy with JHipster and Micronaut
 
Groovy for Java Devs
Groovy for Java DevsGroovy for Java Devs
Groovy for Java Devs
 
Grails Launchpad - From Ground Zero to Orbit
Grails Launchpad - From Ground Zero to OrbitGrails Launchpad - From Ground Zero to Orbit
Grails Launchpad - From Ground Zero to Orbit
 
Room with a Vue - Introduction to Vue.js
Room with a Vue - Introduction to Vue.jsRoom with a Vue - Introduction to Vue.js
Room with a Vue - Introduction to Vue.js
 
Shields Up! Securing React Apps
Shields Up! Securing React AppsShields Up! Securing React Apps
Shields Up! Securing React Apps
 

Using React with Grails 3

  • 1. Using React with Grails 3 Zachary Klein Software Engineer, OCI
  • 3. About Me Grails & web developer for 5 years Joined OCI Grails team in 2015 Using React since 2015 Author of the React profile/s for Grails 3 @zacharyaklein On the Grails Team at @ObjectComputing. Christ-follower, husband (of Elizabeth), dad (of John & Timmy) programmer, guitarist. Needing to be more like Jesus.
  • 5. Where we’re going today • What is React? • Brief overview of React and friends (npm, webpack, babel) • Only a primer - there’s lots more! • How do I use React with Grails? • Asset Pipeline plugin/s • React profile - monolith • React profile 2.x - separate client & server • Isomorphic React - server-side rendering w/Nashorn • Resources & Q/A
  • 6. What is React? A Javascript library for building user interfaces Key Features: • Virtual DOM • JSX • Components • Single Page Apps • Functional emphasis • Beyond the browser “React is a JavaScript library,and so it assumes you have a basic understanding of the JavaScript language.”
  • 7. What is React? import React from ‘react’ import ReactDOM from ‘react-dom’ ReactDOM.render( <h1>Hello, world!</h1>, document.getElementById('root') );
  • 8. What is React? import React from ‘react’ import ReactDOM from ‘react-dom’ ReactDOM.render( <h1>Hello, world!</h1>, document.getElementById('root') );
  • 9. What is React? import React from ‘react’ import ReactDOM from ‘react-dom’ class Greeting extends React.Component { render() { return <h1>Hello, {this.props.name}!</h1>; } } ReactDOM.render( <Greeting name=‘G3 Summit’ />, document.getElementById('root') );
  • 10. What is React? var React = require(‘react’); var ReactDOM = require(‘react-dom’); var Greeting = React.createClass({ render: function() { return <h1>Hello, {this.props.name}</h1>; } }); ReactDOM.render( <Greeting name=‘G3 Summit’ />, document.getElementById('root') );
  • 11. What is React? import React from ‘react’ import ReactDOM from ‘react-dom’ class Greeting extends React.Component { render() { return <h1>Hello, {this.props.name}!</h1>; } } ReactDOM.render( <Greeting name=‘G3 Summit’ />, document.getElementById('root') );
  • 12. What is React? var React = require(‘react’); var ReactDOM = require(‘react-dom’); var Greeting = React.createClass({ render: function() { return React.createElement( 'h1', null, `Hello, ${this.props.name}!` ); } }); ReactDOM.render( React.createElement(Greeting, {name: ‘G3 Summit'}, null), document.getElementById('root') );
  • 13. What is React? var React = require(‘react’); var ReactDOM = require(‘react-dom’); var Greeting = React.createClass({ render: function() { return React.createElement( 'h1', null, `Hello, ${this.props.name}!` ); } }); ReactDOM.render( React.createElement(Greeting, {name: ‘G3 Summit'}, null), document.getElementById('root') ); name bodyprops name bodyprops
  • 14. What is React? import React from ‘react’ import ReactDOM from ‘react-dom’ class Greeting extends React.Component { render() { return <h1>Hello, {this.props.name}!</h1>; } } ReactDOM.render( <Greeting name=‘G3 Summit’ />, document.getElementById('root') ); name body propsname
  • 15. What is React? class Greeting extends React.Component { render() { return( React.createElement('div', {}, React.createElement('h1', {}, “Greetings, ${this.props.name}"), React.createElement('ul', {}, React.createElement('li', {}, React.createElement('a', {href: ‘edit'}, ‘Edit this greeting’) ), React.createElement('li', {}, React.createElement('a', {href: 'reset'}, ‘Reset this greeting') ) ) ); } } ReactDOM.render( React.createElement(Greeting, {name: ‘G3 Summit'}, null), document.getElementById('root') );
  • 16. What is React? class Greeting extends React.Component { render() { return (<div> <h1>Hello, {this.props.name}!</h1> <ul> <li><a href=‘edit’>Edit this greeting</a></li> <li><a href=‘reset’>Reset this greeting</a></li> </ul> </div>); } } ReactDOM.render( <Greeting name=‘G3 Summit’ />, document.getElementById('root') );
  • 17. What is React? “React is only the view layer. We're only in one concern. React only knows how to render markup. It doesn't know where your data came from, how it's stored, or how to manipulate it. What concerns are being violated?” Andrew Ray, via http://blog.andrewray.me/youre-missing-the-point-of-jsx/
  • 18. Component State class Greeting extends React.Component { constructor() { super(); this.state = { greetings: ['Hello', 'Salutations', 'Ho there’] } } render() { const greetings = this.state.greetings; const randomGreeting = greetings[Math.floor(Math.random() * greetings.length]; return( <h1>{randomGreeting}, {this.props.name}</h1> ); } } ReactDOM.render( React.createElement(<Greeting name=‘G3 Summit’ />, document.getElementById('root') );
  • 19. Component State <Greeting 📊 /> <App 📊 /> <Greeting data={⬆ }/> Redux.createStore( 📊) <App ⬆ /> <Greeting ⬆ /> <App 📊 /> Each component? Top-level component? External store? Where should state go? 📊
  • 20. Component State <App 📊 /> <Greeting data={⬆ }/> Recommendation: Store all state in top-level component Pass state down to children components via props 📊
  • 21. What is React? Other features: • Lifecycle methods • Event-handling • State management • PropType checking • DOM access via refs • SPA or partials “React is, in our opinion,the premier way to build big, fast Web apps with JavaScript. It has scaled very well for us at Facebook and Instagram.”
  • 22. What is React? https://www.toptal.com/react/navigating-the-react-ecosystem React Bootstrap React is a small, focused library by design, but there’s plenty of options for augmentation. fetch axios
  • 23. What is React? React can be used standalone, but is more frequently used with node, npm & friends.
  • 24. What is React? “Node.js is an open-source, cross-platform JavaScipt runtime environment.” (Wikipedia)
  • 25. What is React? “npm is the default package manager for… Node.js.” (Wikipedia)
  • 26. What is React? The 6th edition of ECMAScript, officially titled ECMAScript 2015.
  • 27. What is React? Babel - a Javascript “transpiler”
  • 28. What is React? Webpack is an open-source Javascript module bundler. (official website)
  • 29. Why use React with Grails?
  • 30. Why use React with Grails? • Rest support • rest-api profile • JSON views • Spring Websockets • Gradle +
  • 31. How do I use React with Grails? • Most React community resources & documentation assume a node/Javascript-based environment. • Typical project structure is not immediately transferable to a Grails project. • Use of npm is ubiquitous (but not required) and brings both advantages & challenges. • Potential tension between front-end and back-end developer workflows • Wild west - the trail is still being blazed!
  • 32. How do I use React with Grails? What build system would you like to see? Other 6% grails-app/assets 40% src/main/webapp 27% Separate app 27% Where should the client app live? Other 8% Asset Pipeline 48% Webpack 34% SystemJS 10%
  • 33. React & the Asset Pipeline
  • 34. React & the Asset Pipeline • Front-end asset management/processing in Grails is typically handled via the Asset Pipeline. • Recently-released plugins now support React with AP. • React/JSX files in grails-app/assets/javascripts. • Take advantage of other front-end Grails/Gradle plugins, such as the excellent client-dependencies plugin (https://github.com/craigburke/client- dependencies-gradle) • Performant, extensible, familiar to Grails developers.
  • 35. React & the Asset Pipeline • Babel Asset-Pipeline Plugin • Makes use of the Babel transpiler to transform ES6 code to ES5 • Supports Webpack bundler, hot-reloading • Promising, but tricky to configure compile(‘:babel-asset-pipeline:2.1.0')
  • 36. React & the Asset Pipeline • JSX Asset-Pipeline Plugin • Natively parses React/JSX code w/o Babel • Follows Asset Pipeline conventions • Works well with client-dependencies plugin • Documentation is lacking • Excellent option for teams experienced/invested in AP
 assets "com.bertramlabs.plugins:jsx-asset-pipeline:2.12.0"
  • 37. React & the Asset Pipeline DEMO
  • 38. React & the Asset Pipeline • “Asset pipeline” seems to be out of favor among front-end developers (perhaps unfairly). • May be difficult to follow along with community documentation/resources. • AP support for new asset processing tools tends to lag behind Node-based tooling. • Not compatible with popular front-end toolchains. • Grails-centric - may be confusing/unfamiliar to a dedicated front-end team.
  • 39. Webpack • Webpack is a Module bundler. • Typically installed via npm, run via scripts in package.json • Designed for Single Page Apps • Supports a huge variety of asset types via configurable “loaders” (processors) • Supports code-splitting/chunking • Supports hashed/version assets for cache control • Outputs a single bundle (by default), containing all required Javascript/CSS to render the target (i.e, your React app)
  • 40.
  • 41. import ‘text-label’ _react2.default.createElement( _reactBootstrap.Modal.Footer, null, _react2.default.createElement( 'span', { className: 'copyright' }, 'Built with React 15.3.2, webpack 1.13.1, Grails 3' ), _react2.default.createElement( _reactBootstrap.Button, { onClick: this.toggleModal }, 'Close' ) hello.js bundle.js import ‘lodash’ text-label.js //from node_modules loadash.js import ‘hello’ app.js
  • 42. function hello() { return <div>Hello world!</div>; } "use strict"; function hello() { return React.createElement( "div", null, "Hello world!" ); } hello.js bundle.js - loader
  • 43. Asset Pipeline vs Webpack
  • 44. Asset Pipeline vs Webpack
  • 45. Webpack & Grails + src/main/webapp Grails App React App (UI) <RESTAPI>
  • 46. Webpack & Grails + Grails App React App (UI) <RESTAPI>
  • 47. Webpack & Grails • Compromise: • Configure Webpack to output bundle/s into grails-app/assets/ javascripts • Keep React source code in a separate directory tree (e.g, src/ main/webapp) • Rely on Webpack for processing our React/ES6 code • Continue to use AP for non-React assets (jQuery, bootstrap, scaffolded pages, etc), as well as to serve the webpack bundle to the browser. • Use Gradle to automate running webpack via npm scripts • All this can be tricky to configure - if only there was an easier way… +
  • 48. React Profile for Grails 3 grails create-app myReactApp —profile=org.grails.profiles:react:1.0.2 React 1.x Profile generates a Grails project with the following: • React, ReactDOM etc., installed via npm • Webpack configured to process React code and output to grails-app/assets/javascripts • gradle-node plugin installed, custom tasks to run webpack on app startup/packaging • Sample React code & unit tests
  • 49. React Profile for Grails 3 src/main/webapp Grails App React App (UI)
  • 50. React Profile for Grails 3 $ ls -l grails-app/ - assets/ - - javascripts/ - - - bundle.js node_modules/ package.json src/ - main/ - - webapp/ - - - app/ - - - - about.js - - test/ - - - js/ - - - - about.spec.js webpack.config.js npm project file webpack configuration file React source code webpack bundle Unit test
  • 51. React Profile for Grails 3 DEMO
  • 52. React Profile for Grails 3 • The React 1.x profile simplifies the setup process for using React & Webpack with Grails • Designed for monolithic applications • Gradle-node plugin ties the two “worlds” together • Single development/test/deployment path • Gradle wrapper allows front-end devs to run the Grails app w/o installing Grails • Gradle-node tasks allow Grails devs to run webpack w/o installing npm ./gradlew bootRun ./gradlew webpack
  • 53. Separate Client & Server • Microservice-friendly • Deploy/update front-end and back-end separately • Take advantage of Grails’ RESTful features • Domain resources • JSON Views • Gradle multi-project build for client & server apps • Requires fully standalone React app, including: • webpack, babel & dev-server • CORS configuration
  • 54. Separate Client & Server Grails App (server) React App (client) <RESTAPI>
  • 55. create-react-app “Create React App is a new officially supported way to create single-page React applications. It offers a modern build setup with no configuration.” https://facebook.github.io/react/blog/2016/07/22/create-apps-with-no-configuration.html • Generates fully standalone React app • Provides working webpack & Babel config • Provides scripts for starting app, building public bundle, running tests • Simplified development & an easy “exit strategy”
  • 56. React Profile for Grails 3 grails create-app myReactApp —profile=react React 2.x Profile generates a multi-project Gradle build: • React app (generated via create-react-app) as client project • Grails 3 app (rest-api profile) as server project • Gradle-node tasks defined within client project to run npm scripts (start, build, test, & eject) • Grails index page built with react-bootstrap, with app data populated via REST call
  • 57. React Profile for Grails 3 Grails App React App <RESTAPI> client server
  • 58. React Profile for Grails 3 $ ls -l client/ - build.gradle - node_modules/ - package.json - public/ - src/ - - App.js - - App.test.js server/ settings.gradle npm project file Grails 3 app React source code React app Unit test Gradle project file
  • 59. React Profile for Grails 3 DEMO
  • 60. Isomorphic React • Isomorphic - same code on client & server (aka “universal”) • React can be rendered server-side • Can improve initial page load time • Can improve SEO performance • Java 8 introduced a new JavaScript engine, Nashorn
  • 61. Isomorphic React NashornScriptEngine nashornScriptEngine = new ScriptEngineManager().getEngineByName("nashorn") nashornScriptEngine.eval(readResource(“myscript.js”)) nashornScriptEngine.invokeFunction('myFunction', “${new JsonBuilder([arg:‘some arg’])}”) function myFunction(data) { render(“Here’s some data we got from the server: “ + data.arg); }
  • 63. Isomorphic React • Tightly couples React source code with Grails app • Adds additional complexity • Requires polyfill to work with React • Nashorn's CSS/LESS support is very poor • Perhaps useful for React scaffolding? • Performance?
  • 64. Resources React Ecosystem • https://www.toptal.com/react/navigating-the-react-ecosystem JSX • http://jamesknelson.com/learned-stop-worrying-love-jsx/ • http://blog.andrewray.me/youre-missing-the-point-of-jsx/ React Architecture • https://facebook.github.io/react/docs/thinking-in-react.html • https://discuss.reactjs.org/t/best-practices-for-extending- subclassing-components/1820
  • 65. Resources Grails Plugins & Profiles • https://grails.org/plugin/babel-asset-pipeline • https://github.com/bertramdev/asset-pipeline/tree/master/jsx-asset- pipeline • https://github.com/ZacharyKlein/grails-isomorphic • https://github.com/grails-profiles/webpack • https://github.com/grails-profiles/react Using React & Grails • http://grailsblog.objectcomputing.com/posts/2016/05/28/using-react- with-grails.html • http://grailsblog.objectcomputing.com/posts/2016/11/14/ introducing-the-react-profile-for-grails.html • http://guides.grails.org/using-the-react-profile/guide/index.html