Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Bring your web application architecture to the ...
Search
Bastian Hofmann
June 11, 2015
Programming
240
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Bring your web application architecture to the next level with React.JS
Bastian Hofmann
June 11, 2015
More Decks by Bastian Hofmann
See All by Bastian Hofmann
Monitoring in Kubernetes with Prometheus and Grafana
bastianhofmann
0
370
Creating a fast Kubernetes Development Workflow
bastianhofmann
0
150
Highly available cross-region deployments with Kubernetes
bastianhofmann
1
180
From source to Kubernetes in 30 minutes
bastianhofmann
0
200
Introduction to Kubernetes
bastianhofmann
1
140
CI/CD with Kubernetes
bastianhofmann
0
250
Creating a fast Kubernetes Development Workflow
bastianhofmann
1
290
Deploying your first Micro-Service application to Kubernetes
bastianhofmann
2
220
Creating a fast Kubernetes Development Workflow
bastianhofmann
0
280
Other Decks in Programming
See All in Programming
iOS開発×AI駆動開発 〜最近使って便利だったスキルの話〜
nogu66
0
140
kubernetes コンポーネント開発入門 / 新卒N年目の勉強会&交流会!〜〇〇への誘い〜 #n_study
mazrean
0
170
Family mrubyの進捗
kishima
1
100
Hello, Hiroshima Geospatial Data! — Exploring DoboX with Python
ra0kley
0
180
異なる設計思想のフレームワークを経験して得た学び
amekuhideki
2
1.5k
GraphRAGのKnowledge Graphを 直接!見る/View-GraphRAG's-KnowledgeGraph-directly!
tyumugi1113
0
240
Press start. Python's next generation.
willingc
PRO
3
320
Oxlintはいいぞ(続)
yug1224
1
560
スマート反転とウェブアクセシビリティ
camiha
0
140
業務時間外もAIに働いてもらう話
colorful12
3
10k
デプロイ直後のレイテンシスパイクを調べたら、 Railsの仕様にたどり着いた
nhsykym
0
110
信頼性の目標を誰も求めてない
shubox
0
490
Featured
See All Featured
Raft: Consensus for Rubyists
vanstee
141
7.7k
Mobile First: as difficult as doing things right
swwweet
225
10k
AI: The stuff that nobody shows you
jnunemaker
PRO
9
980
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.5k
Optimising Largest Contentful Paint
csswizardry
37
3.9k
Faster Mobile Websites
deanohume
310
32k
Prompt Engineering for Job Search
mfonobong
0
440
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
250
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
510
We Are The Robots
honzajavorek
0
350
Odyssey Design
rkendrick25
PRO
2
800
The Pragmatic Product Professional
lauravandoore
37
7.4k
Transcript
Bring your web application to the next level with React.JS
@BastianHofmann
Let's talk about...
Application architecture
Code and component re-use
Rapid Development
Handling large code-bases
With React.js
With small and independent components
webserver HTML browser JS controller
webserver HTML browser JS Ajax controller
webserver HTML browser JS Ajax controller
de duplication ode duplication code duplication code duplication code duplication
code duplication code duplication code duplication code duplication code duplication code duplication code duplication code duplication code duplicatio code duplicat code duplic code dup code du code cod co co
So?
A few words about me
None
None
http://speakerdeck.com/u/bastianhofmann
Application architecture
many roads
None
status quo
webserver HTML browser JS Ajax controller
webserver loadbalancer pgsql memcached mongodb services
webserver
None
MVC
Duplication and only some Code re- use
We can do better
Small components over big controllers
None
None
None
None
None
None
None
None
Self contained
Can be used everywhere
JS is part of the component
CSS can be part of the component
http://facebook.github.io/react/
JavaScript UI Library
Many small, self contained, reusable components
var HelloMessage = React.createClass( { render: function () { return
<div>Hello {this.props.name}</div>; } } ); React.render(<HelloMessage name="John" />, mountNode);
var Timer = React.createClass({ getInitialState: function () { return {secs:
0}; }, tick: function () { this.setState({secs: this.state.secs + 1}); }, componentDidMount: function () { this.interval = setInterval(this.tick, 1000); }, componentWillUnmount: function () { clearInterval(this.interval); }, render: function () { return (<div> Seconds Elapsed: {this.state.secs}</div> ); } }); React.render(<Timer />, mountNode);
var TodoList = React.createClass( { render: function () { var
createItem = function (itemText) { return <li>{itemText}</li>; }; return ( <ul> {this.props.items.map(createItem)} </ul> ); } } );
var TodoApp = React.createClass({ render: function () { return (
<div> <h3>TODO</h3> <TodoList items={this.props.items} /> </div> ); } });
Getting the data in there
PHP Backend JSON React Frontend
PHP Backend JSON React Frontend
PHP Backend JSON React Frontend
PHP Backend JSON React Frontend
var TodoApp = React.createClass({ getInitialState: function () { return {items:
[…]}; }, render: function () { return ( <div> <h3>TODO</h3> <TodoList items={this.state.items} /> </div> ); } });
Problematic
Remember
Self-contained
Reusable
PHP Backend JSON React Frontend
PHP Backend JSON React Frontend
PHP Backend JSON React Frontend
Overfetching
Underfetching
Solutions
Flux Datastores
PHP Backend React Frontend Datastores
GraphQL https://gist.github.com/wincent/598fa75e22bdfa44cf47
Requirements
Profile Publications Publication Publication Publication AboutMe LeftColumn Image Institution Menu
React Frontend
Profile Publications Publication Publication Publication AboutMe LeftColumn Image Menu PHP
Backend Request Response Institution
LeftColumn Image Menu React Frontend
LeftColumn Image Menu Request Response PHP Backend
Remember: self contained
Profile Publications Publication Publication Publication AboutMe LeftColumn Image Menu Account
Account Account Account Account Publication1 Publication2 Publication3 Institution
Do not fetch data directly
Sssssssllllooooowww
Require stuff
http://www.infoq.com/presentations/Evolution-of-Code- Design-at-Facebook/
None
Widget Widget Widget Widget Preparer Resolver Resolver Services Connector Interfaces
Connector Implementations
Widget Widget Widget Widget Preparer Fetch Requirements Resolver Resolver Services
Connector Interfaces Connector Implementations
Widget Widget Widget Widget Preparer Resolver Resolver Services Connector Interfaces
Connector Implementations Batch requirements and pass them to resolvers
Widget Widget Widget Widget Preparer Resolver Resolver Services Connector Interfaces
Connector Implementations Call Services as effective as possible (Multi-GET,...)
Widget Widget Widget Widget Preparer Resolver Resolver Services Connector Interfaces
Connector Implementations Attach fetched data to Requirements and pass them back to the preparer
Widget Widget Widget Widget Preparer Resolver Resolver Services Connector Interfaces
Connector Implementations Distribute fetched data to the widgets that required it
public function collect() { return [ new EntityRequirement( 'account', Account::class,
['id' => $this->request->get('id')] ), ]; }
Request Cache
Multi-GET
Futures
Data dependencies within a widget
public function collect() { yield [ new EntityRequirement( 'account', Account::class,
['id' => $this->request->get('id')] ), ]; yield [ new ServiceRequirement( 'scienceDisciplines', AccountService::class, 'getScienceDisciplines', ['account' => $this->account] ) ]; }
Assembling the tree
Profile Publications Publication Publication Publication AboutMe LeftColumn Image Menu PHP
Backend Request Response Institution
Widget Requirement
class PublicationKeywordSearch { public $publications; public $publicationListItems = [];
public function collect() { yield [ serviceRequirement( 'publications', PublicationService::getCall()->getByKeywords( ['virology', 'cancer'], 10, 0 ) ) ]; foreach ($this->publications as $publication) { yield new WidgetRequirement( 'publicationListItems', PublicationItem::CLASS, [ 'publicationId' => $publication->getId() ] ); } } }
class PublicationItem { public $publicationId; public $publication; public
function collect() { yield new RequestDataRequirement('publicationId'); yield [ new EntityRequirement( 'publication', Publication::class, ['id' => $this->publicationId] ) ]; } }
Profile Publications Publication Publication Publication AboutMe LeftColumn Image Menu Institution
Getting the data to React.js
public function getData() { return [ 'key' => 'value', 'other'
=> 'data', 'number' => 42, 'subComponent' => $this->subComponent, 'someArray' => [1, 2, 3], 'subComponentList' => [ $this->items[0], $this->items[1] ] ]; }
{ "ParentComponent-1234": { "component": "ParentComponent.jsx", "id": "ParentComponent-1234", "data": { "key":
"value", "other": "data", "number": 42, "subComponent": "SubComponent", "someArray": [1, 2, 3], "subComponentList": [ "ListItem-444", "ListItem-555"] } }, "SubComponent": { "component": "SubComponent.jsx", "id": "SubComponent", "data": { "boolValue": true } }, "ListItem-444": { "component": "ListItem", "id": "ListItem-444", "data": { "title": "444s title“ } }, "ListItem-555": { "component": "ListItem", "id": "ListItem-555", "data": { "title": "555s title" } } }
var React = require('react'); var SubComponent = require('SubComponent'); var ListItem
= require('ListItem'); module.exports = React.createClass( { displayName: 'ParentComponent', render: function () { var items = []; for (var key in this.props.subComponentList) { if (this.props.subComponentList.hasOwnProperty(key)) { items.push( <li> <ListItem componentId={this.props.subComponentList[key]}/> </li> ); } } return ( <div className="{this.props.key}"> <h1>{this.props.other} Hello PHP {this.props.number}</h1> <SubComponent componentId={this.props.subComponent} /> <ul>{items}</ul> </div> ); } } );
var React = require('react'); module.exports = React.createClass( { displayName:
'ListItem', render: function () { return ( <div> {this.props.title} </div> ); } } );
Server side rendering
SEO
Better user experience
Several approaches
NodeJS service
Request HTML Preparer nodeJS service PHP process Data JSON Rendered
HTML string React Hogan
Bundling
http://webpack.github.io/
None
Whoa
This looks awfully complicated to debug
None
None
Benefits
Enables developers to only focus on their components
Rapid prototyping
Easier Refactoring
Easy re-using of components
Error Handling
Profile Publications Publication Publication Publication AboutMe LeftColumn Image Menu Institution
Profile Publications Publication Publication Publication AboutMe LeftColumn Image Menu EXCEPTION
Institution
Profile Publications Publication Publication Publication LeftColumn Image Menu Institution
Experiments (A/B Testing)
Feature toggles
Profile Publications Publication Publication Publication AboutMe LeftColumn Image Menu Institution
Profile Publications Publication Publication Publication AboutMeNew LeftColumn Image Menu Institution
Caching of Components
Profile Publications Publication Publication Publication AboutMe LeftColumn Image Menu <esi:include
src="..." /> Institution
Load components asynchronously
Profile Publications Publication Publication Publication AboutMe LeftColumn Image Menu <div
id="placeholder"></div> <script>loadWidget('/aboutMe', function(w) { w.render({ replace : '#placeholder' }); })</script> Institution
Conclusion?
Think about your architecture
Refactor and make it better continuously
Frontend and backend are part of the same application
Don't rewrite your whole codebase in one go
http://twitter.com/BastianHofmann http://lanyrd.com/people/BastianHofmann http://speakerdeck.com/u/bastianhofmann
[email protected]