+ All Categories
Home > Technology > Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

Date post: 16-Apr-2017
Category:
Upload: ryan-weaver
View: 6,849 times
Download: 4 times
Share this document with a friend
103
Finally, Professional Frontend dev with: ReactJS, Webpack & Symfony ’s
Transcript
Page 1: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

Finally, Professional Frontend dev with:

ReactJS, Webpack & Symfony

♥’s

Page 2: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

> Lead of the Symfony documentation team

> KnpLabs US - Symfony consulting, training & kumbaya

> Writer for KnpUniversity.com: PHP & Symfony screencasts packed with puns, unrelated (but entertaining) illustrations and coding challenges!> Husband of the much more talented @leannapelham

knpuniversity.com twitter.com/weaverryan

¡Hola!

Page 3: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

♥’s

Finally, Professional Frontend dev with:

ReactJS, Webpack & Symfony

Page 4: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

, ReactJS, webpack

@weaverryan

All of Modern JavaScript in 45 minutes!

ES6

the 12 new JS things they invent during this presentation

, ES2015 , ECMAScript

, Babel

, NodeJS

npm , JSX …

… and of course …

Page 5: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

Modern JavaScript is a lot like…

@weaverryan

Game of Thrones

Page 6: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

JavaScript

@weaverryan

GoT

Countless libraries and competing standards fighting

for influence

Countless characters and completing factions fighting

for influence

Page 7: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

@weaverryan

You spent 6 months building your site in <Cool.JS> only

to read on Twitter that: “no self-respecting dev

uses that crap anymore”

That character you love and followed for 2 seasons, was

just unceremoniously decapitated

JavaScript

GoT

Page 8: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

@weaverryan

Plain, boring old JavaScript

Page 9: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

JavaScript is a (weird) language

IT JUST HAPPENS THAT BROWSERS CAN EXECUTE THAT LANGUAGE

Page 10: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

@weaverryan

// yay.jsvar message = 'I like Java...Script'; console.log(message);

> node yay.js

I like Java...Script

NodeJS: server-side JavaScript engine

npm: Composer for NodeJS

Page 11: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

@weaverryan

Follow along with the real code:

github.com/weaverryan/symfonycat-js

(hint: look at the history, each thing we do is its own commit)

Page 12: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

// web/js/productApp.jsvar products = [ 'Sheer Shears', 'Wool Hauling Basket', 'After-Shear (Fresh Cut Grass)', 'After-Shear (Morning Dew)']; var loopThroughProducts = function(callback) { for (var i = 0, length = products.length; i < length; i++) { callback(products[i]); }}; loopThroughProducts(function(product) { console.log('Product: '+product);});

{# app/Resources/views/default/products.html.twig' #}

<script src="{{ asset('js/productApp.js') }}"></script>

our store for sheep (baaaa)

Page 13: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)
Page 14: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

class ProductCollection{ constructor(products) { this.products = products; }} let collection = new ProductCollection([ 'Sheer Shears', 'Wool Hauling Basket', 'After-Shear (Fresh Cut Grass)', 'After-Shear (Morning Dew)', ]);let prods = collection.getProducts();let loopThroughProducts = function(callback) { for (let i = 0, length = prods.length; i < length; i++) { callback(collection.getProduct(i)); }}; loopThroughProducts(product => console.log('Product: '+product));

what language is this?

JavaScript

Page 15: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

@weaverryan

ECMAScriptThe official name of standard JavaScript

ES6/ES2015/HarmonyThe 6th accepted (so official) version of ECMAScript

Page 16: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

class ProductCollection{ constructor(products) { this.products = products; }} let collection = new ProductCollection([ 'Sheer Shears', 'Wool Hauling Basket', 'After-Shear (Fresh Cut Grass)', 'After-Shear (Morning Dew)', ]);let prods = collection.getProducts();let loopThroughProducts = function(callback) { for (let i = 0, length = prods.length; i < length; i++) { callback(collection.getProduct(i)); }}; loopThroughProducts(product => console.log('Product: '+product));

But will it run in a browser???

Maybe!

Page 17: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

class ProductCollection{ constructor(products) { this.products = products; }} let collection = new ProductCollection([ 'Sheer Shears', 'Wool Hauling Basket', 'After-Shear (Fresh Cut Grass)', 'After-Shear (Morning Dew)', ]);let prods = collection.getProducts();let loopThroughProducts = function(callback) { for (let i = 0, length = prods.length; i < length; i++) { callback(collection.getProduct(i)); }}; loopThroughProducts(product => console.log(product));

Proper class and inheritance syntax

let: similar to var, but different

function (product) { console.log(product); }

Page 18: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

Now we just need to wait 5 years for the crappiest browsers

to support this

Page 19: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

@weaverryan

Babel

… or do we?

A JS transpiler!

Page 20: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

Babel is a NodeJS binary…

{ "name": "js-tutorial", "version": "1.0.0"}

1) Make a package.json file

2) Download babel

> npm install --save-dev babel-cli

@weaverryan

Page 21: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

> ./node_modules/.bin/babel \ web/js/productApp.js \ -o web/builds/productApp.js

{# app/Resources/views/default/products.html.twig' #}

<script src="{{ asset('builds/productApp.js') }}"></script>

@weaverryan

Page 22: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

> ./node_modules/.bin/babel \ web/js/productApp.js \ -o web/builds/productApp.js

{# app/Resources/views/default/products.html.twig' #}

<script src="{{ asset('builds/productApp.js') }}"></script>

But, this made no changes

js/productApp.js == builds/productApp.js

@weaverryan

Page 23: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

Babel can transpile anything

CoffeeScript --> JavaScript

Coffee --> Tea

ES6 JS --> ES5 JS

* Each transformation is called a preset

Page 24: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

1) Install the es2015 preset library

2) Add a .babelrc file

> npm install --save-dev babel-preset-es2015

{ "presets": [ "es2015" ]}

@weaverryan

Page 25: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

> ./node_modules/.bin/babel \ web/js/productApp.js \ -o web/builds/productApp.js

loopThroughProducts( product => console.log('Product: '+product));

loopThroughProducts(function (product) { return console.log('Product: ' + product);});

source:

built:

Page 26: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

But we can use new (or experimental) features now

@weaverryan

Modern JavaScript has a build step

Big Takeaway #1:

Page 27: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

@weaverryan

New to ES6:

JavaScript Modules!

Page 28: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

The Classic Problem:

If you want to organize your JS into multiple files, you need to manually

include all those script tags!

@weaverryan

Page 29: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

// web/js/ProductCollection.js class ProductCollection{ constructor(products) { this.products = products; } getProducts() { return this.products; } getProduct(i) { return this.products[i]; }} export ProductCollection;

@weaverryan

Page 30: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

// web/js/productApp.jsimport ProductCollection from './ProductCollection';var collection = new ProductCollection([ 'Sheer Shears', 'Wool Hauling Basket', 'After-Shear (Fresh Cut Grass)', 'After-Shear (Morning Dew)',]);// ...

{# app/Resources/views/default/products.html.twig' #}

<script src="{{ asset('builds/productApp.js') }}"></script>

Page 31: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

// web/js/productApp.jsimport ProductCollection from './ProductCollection';var collection = new ProductCollection([ 'Sheer Shears', 'Wool Hauling Basket', 'After-Shear (Fresh Cut Grass)', 'After-Shear (Morning Dew)',]);// ...

{# app/Resources/views/default/products.html.twig' #}

<script src="{{ asset('builds/productApp.js') }}"></script>

> ./node_modules/.bin/babel \ web/js/productApp.js \ -o web/builds/productApp.js

Page 32: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)
Page 33: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

Module loading in a browser is hard to do

@weaverryan

Page 34: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

@weaverryan

Introducing…

Page 35: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

@weaverryan

Webpack!

• bundler • module loader • all-around nice guy

Page 36: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

Install webpack

> npm install --save-dev webpack

@weaverryan

Page 37: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

Use require instead of import/export *

* I’ll tell you why later

// web/js/ProductCollection.jsclass ProductCollection{ // ...} module.exports = ProductCollection;

// web/js/productApp.jsvar ProductCollection = require('./ProductCollection');// ...

Page 38: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

Go webpack Go!

> ./node_modules/.bin/webpack \ web/js/productApp.js \ web/builds/productApp.js

The one built file contains the code from both source files

Page 39: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

Optional config to make it easier to use:

// webpack.config.jsmodule.exports = { entry: { product: './web/js/productApp.js' }, output: { path: './web/builds', filename: '[name].js', publicPath: '/builds/' }};

builds/product.js

{# app/Resources/views/default/products.html.twig' #}

<script src="{{ asset('builds/product.js') }}"></script>

Page 40: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

> ./node_modules/.bin/webpack

@weaverryan

Page 41: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

Wait!

We lost our ES6->ES5 transformation!!!

@weaverryan

Page 42: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

Hey webpack!

Yo! When you load .js files, can you run them through Babel for me?

- kthxbai <3 Ryan

webpack loaders allow you to transform files as they’re loaded

Page 43: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

1) Install the babel-loader

2) Activate the loader in webpack.config.js

> npm install --save-dev babel-loader

module.exports = { // ... module: { loaders: [ { test: /\.js$/, loader: "babel-loader", exclude: /node_modules/ } ] }};

Page 44: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

> ./node_modules/.bin/webpack

@weaverryan

Page 45: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

Module loading +

ES6 Support

Page 46: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

Use import/export now if you prefer

// web/js/ProductCollection.jsclass ProductCollection{ // ...} export default ProductCollection;

// web/js/productApp.jsimport ProductCollection from './ProductCollection'; // ...

Page 47: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

> ./node_modules/.bin/webpack

@weaverryan

Page 48: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

@weaverryan

Dev Tools

… because life is too short to run webpack after every change you make

Page 49: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

> npm install webpack-dev-server --save-dev

> ./node_modules/.bin/webpack-dev-server \ --content-base=./web/

@weaverryan

1) Install the webpack-dev-server

2) Run that!

Page 50: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

http://localhost:8080 - static assets are served - compiled assets are served dynamically

Page 51: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

Wait!

@weaverryan

Don’t I need to update all my script tags?

<script src="{{ asset('builds/product.js') }}">

<script src="http://localost:8080/builds/product.js">

Page 52: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

# app/config/config.ymlframework: assets: base_url: http://localhost:8080

Boom!

@weaverryan

Page 53: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

… or the real solution

@weaverryan

# app/config/parameters.ymlparameters: # ... use_webpack_dev_server: true

Page 54: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

class AppKernel extends Kernel{ // ... public function registerContainerConfiguration(LoaderInterface $loader) { // ... $loader->load(function(ContainerBuilder $container) { if ($container->getParameter('use_webpack_dev_server')) { $container->loadFromExtension('framework', [ 'assets' => [ 'base_url' => 'http://localhost:8080' ] ]); } }); }}

… or the real solution

@weaverryan

Page 55: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

@weaverryan

Status Updatewe can:

• use ES6 features • import and export modules

Page 56: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

@weaverryan

CSS: An un-handled dependency of your JS app

Page 57: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

Could we do this?

// web/js/productApp.jsimport ProductCollection from './ProductCollection'; // could this somehow load that CSS for us?import '../css/productApp.css'; // ...

Loader!

Page 58: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

module.exports = { // ... module: { loaders: [ { test: /\.js$/, exclude: /node_modules/, loader: "babel-loader" } ] }};

webpack loaders allow you to transform files as they’re loaded

Remember:

Page 59: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

1) Install the css-loader

2) Activate the loader just for this file

> npm install css-loader --save-dev

import 'css!../css/productApp.css';

this transforms the CSS into a JS data-structure… but does nothing with it

Page 60: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

1) Install the style-loader

> npm install style-loader --save-dev

import 'style!css!../css/productApp.css';

inlines the CSS on the page in a style tag

2) Activate both loaders for this file

Page 61: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

Yes,

@weaverryan

the one JS file now holds the contents of two JS files and a CSS file

{# app/Resources/views/default/products.html.twig' #}

<script src="{{ asset('builds/product.js') }}"></script>

Page 62: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

Move the loader to config to simplify

import '../css/productApp.css';

// webpack.config.js module.exports = { // ... module: { loaders: [ // ... { test: /\.css$/, loader: "style!css" } ] },};

Page 63: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

@weaverryan

Ah, but what should my image paths look like?

/* productApp.css */.product-price { color: green; background-image: url('../images/logo.png'); }

http://example.com/products/5/photos/../images/logo.png

http://example.com/products/5/photos

Page 64: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

This broke webpack!

Yes, webpack parses the CSS and tries to load file imports and url() references

(i.e. to images & fonts)

Page 65: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

1) Install the file-loader & url-loader

> npm install file-loader url-loader --save-dev

2) Activate the loader for .png files

// webpack.config.js // ... loaders: [ // ... { test: /\.png/, loader: "url-loader?limit=10000" }]

Page 66: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

{ test: /\.png/, loader: "url-loader?limit=10000"}

For .png files < 10kb

image is turned into a “data url” and inlined in the CSS

For .png files > 10kb

image is copied to builds/ and the new URL is written into the CSS

Page 67: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

Stop

@weaverryan

thinking of your JavaScript as random code that executes

Page 68: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

Start

@weaverryan

thinking of your JavaScript as a single application with dependencies

that are all packaged up together

Page 69: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

@weaverryan

Unleashing the Power of NodeJS and ReactJS

Page 70: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

Like Composer, NodeJS has a lot of third-party libraries

@weaverryan

… and we can use them

Page 71: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

lodash

@weaverryan

JavaScript utility library

Page 72: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

1) Install it

> npm install lodash --save-dev

2) Use it

// web/js/productApp.js// ...import _ from 'lodash'; _.each(collection.getProducts(), function(product) { // ...});

@weaverryan

'./ProductCollection'

vs

'lodash'

Page 73: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

@weaverryan

ReactJS

Page 74: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

// web/js/productApp.jsimport React from 'react'; import ReactDOM from 'react-dom'; var ProductApp = React.createClass({ render: function() { return ( <h1>Yay!</h1> ) }});

??????

$(document).ready(function() { ReactDOM.render( <ProductApp/>, document.getElementById('product-app') );});

Page 75: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

<ProductApp myName="Ryan" />

JSX

React.createElement( ProductApp, { myName: "Ryan" })

This is not real EcmaScript, but babel can handle it

Page 76: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

… but it doesn’t yet

Page 77: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

1) Install the babel preset

> npm install --save-dev babel-preset-react

2) Add the preset in .babelrc

@weaverryan

{ "presets": [ "es2015", "react" ]}

Page 78: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

… nope - still not working

Page 79: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

// productApp.jsimport React from 'react'; import ReactDOM from 'react-dom'; var ProductApp = React.createClass({ render: function() { return ( <h1>Yay!</h1> ) }});$(document).ready(function() { ReactDOM.render( <ProductApp/>, document.getElementById('product-app') );});

Page 80: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

> npm install --save-dev react react-dom

… nope - still not working

Page 81: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

It’s alive, but huge!

*file size would be much smaller in reality,due to missing uglify and other production settings

Page 82: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

@weaverryan

ReactJS is pretty easy

The setup to get here is tough

Page 83: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

React-101: props

@weaverryan

// web/js/Components/ProductList.jsimport React from 'react'; var ProductList = React.createClass({ // ...});module.exports = ProductList;

Page 84: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

// web/js/productApp.jsimport ReactDOM from 'react-dom'; import ProductList from './Components/ProductList'; $(document).ready(function() { ReactDOM.render( <ProductList message="Great Products!" />, document.getElementById('product-app') );});

It’s a prop!

Page 85: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

// web/js/Components/ProductList.jsimport React from 'react'; var ProductList = React.createClass({ render: function() { return ( <h1>{this.props.message}</h1> ) }});module.exports = ProductList;

hello again prop!

Page 86: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

collection props

@weaverryan

// web/js/productApp.jsvar startingProducts = [ 'Sheer Shears', 'Wool Hauling Basket', 'After-Shear (Fresh Cut Grass)', 'After-Shear (Morning Dew)',]; $(document).ready(function() { ReactDOM.render( <ProductList initialProducts={startingProducts} />, document.getElementById('product-app') );});

Page 87: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

// web/js/Components/ProductApp.jsimport _ from 'lodash'; var ProductList = React.createClass({ render: function() { var productRows = []; _.each(this.props.initialProducts, function(product) { productRows.push( <tr> <td>{product}</td> <td className="product-price"> {Math.round(Math.random()*50)} </td> </tr> ); }); // ... }});

Page 88: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

// web/js/Components/ProductApp.jsimport _ from 'lodash'; var ProductList = React.createClass({ render: function() { var productRows = []; // ... return ( <div> <h1>{this.props.message}</h1> <table className="table"> <tbody>{productRows}</tbody> </table> </div> ) }});

Page 89: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

React 101: initial data

@weaverryan

// web/js/productApp.jsvar startingProducts = [ 'Sheer Shears', 'Wool Hauling Basket', 'After-Shear (Fresh Cut Grass)', 'After-Shear (Morning Dew)',]; $(document).ready(function() { ReactDOM.render( <ProductList initialProducts={startingProducts} />, document.getElementById('product-app') );});

Page 90: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

@weaverryan

{# app/Resources/views/default/products.html.twig #}<script> window.startingProducts = [ 'Sheer Shears', 'Wool Hauling Basket', 'After-Shear (Fresh Cut Grass)', 'After-Shear (Morning Dew)', ];</script>

// web/js/productApp.jsvar startingProducts = window.startingProducts;$(document).ready(function() { ReactDOM.render( <ProductApp initialProducts={startingProducts} />, document.getElementById('product-app') );});

Page 91: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

React 101: state

@weaverryan

Page 92: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

var ProductApp = React.createClass({ render: function() { return ( <div> <h1>{this.props.message}</h1> </div> ) }});

Page 93: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

var ProductApp = React.createClass({ render: function() { return ( <div> <h1>{this.state.message}</h1>

<button onClick={this.handleClick}> New Message </button> </div> ) }});

Page 94: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

var ProductApp = React.createClass({ getInitialState: function() { return { message: 'Product List!'; } } // ...}

Page 95: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

var ProductApp = React.createClass({ render: function() { return ( <div> <h1>{this.state.message}</h1>

<button onClick={this.handleClick}> New Message </button> </div> ) }});

Page 96: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

var ProductApp = React.createClass({ handleClick: function(e) { e.preventDefault(); this.setState({ message: 'New Message!' }) }, // ...}

Page 97: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

@weaverryan

Putting it all together

Page 98: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

@weaverryan

ES6/ES2015/ECMAScript 2015

The newest version of Javascript, not supported by all browsers

Page 99: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

@weaverryan

Babel

A tool that can transform JavaScript to different JavaScript

presets A) ES6 js to “old” JS B) JSX to raw JS

Page 100: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

@weaverryan

Webpack

A tool that follows imports to bundle JavaScript, CSS, and anything else you dream up into one JavaScript package

loaders A) JS through Babel B) CSS to inlined styles C) images copied, paths used

Page 101: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

@weaverryan

ReactJS

A nifty frontend framework where you pass around and render props (immutable)

and state (mutable)

Page 102: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

… but the JavaScript world moves quickly…

Page 103: Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony Cat 2016)

Ryan Weaver @weaverryan

THANK YOU!


Recommended