SlideShare ist ein Scribd-Unternehmen logo
1 von 60
Integrating React.js Into a PHP Application
Slides online at:
@AndrewRota | Dutch PHP Conference 2019
What is React.js?
“A JavaScript library for
building user interfaces”
https://reactjs.org/
React.js has, by far, the greatest
market share of any frontend
framework
Laurie Voss, npm and the future of JavaScript (2018)
...and it’s still growing
Laurie Voss, npm and the future of JavaScript (2018)
Among developers, use of both
PHP and React.js are correlated
Stack Overflow, Developer Survey Results 2019
As developers, as want to build the best interfaces
for our users, and React is arguably one of the best
tools for building modern web UIs.
Andrew Rota
@AndrewRota
Associate Director, Software Engineering
Agenda
● ⚛ Lightning Introduction to React.js
● 🎹 Getting Started with Client-Side Rendered React
● ⚙ Server-Side Rendering Architectures
■ V8Js PHP Extension
■ PHP Requests to a Node.js Service
■ Node.js Requests to PHP
● ✹ Future of React.js SSR
● 💡Takeaways
What can React.js add to a PHP web
application?
How can we integrate React.js into a PHP
web application?
PHP and React.js can complement each
other in a web application
Make views a first-class aspect of your web
application
Client
Model
ControllerView
Client
Model
Controller
View
Flexibility to support “single-page
application” experiences
Frontend frameworks can unlock new
interaction patterns
React.js makes it easy (and fun) to create
and manage rich view logic
What is React.js?
“A JavaScript library for
building user interfaces”
Declarative
‣ Design views as
“components” which accept
props and return React
elements
‣ React will handle rendering
and re-rendering the DOM
when data changes
function Hello(props) {
return <h1>Hello, {props.name}</h1>;
}
Composable
‣ In addition to DOM nodes,
components can also render
other components
‣ You can also render child
components for more
generic “box” components
using props.children.
function Hello(props) {
return <h2>My name is, {props.name}</h2>;
}
function NameBadge(props) {
return (<div>
Welcome to {props.conf} Conference.<br />
{props.children}
</div>)
}
function App() {
return (<div>
<NameBadge conf={'Dutch PHP 2019'}>
<Hello name={'Andrew'} />
</NameBadge>
</div>)
}
Encapsulate State
‣ Components can manage
their own encapsulated state
‣ When state is shared across
components, a common
pattern is to lift that state up
to a common ancestor
‣ Libraries such as Redux or
MobX can help with more
complex state management
import {Component} from "react";
class App extends Component {
state = {count: 0};
handleClick = () => {
this.setState(state => {
return {count: state.count + 1}
});
};
render() {
return <div>
<span>Count: {this.state.count} </span>
<button onClick={this.handleClick}>+</button>
</div>;
}
}
Adding React.js to your PHP site:
the easy way

100% Client-Side Rendering
Render a React App
‣ Start with the root element
on a page, and use
ReactDOM.render to start the
application
const root = document.getElementById('root');
const App = <h1>Hello, world</h1>;
ReactDOM.render(App, root);
Initial page load is
blank
JavaScript
loads
Client-Side Rendered
Incremental Adoption
‣ A 100% react application
would have a single react
root.
‣ Use ReactDOM.render() to
create multiple roots when
converting an application
‣ In general, convert
components from the
“bottom up” of the view tree
But we’ve only partially integrated
React.js into our site...
Enter Server-Side Rendering
What is
server-side
rendering (SSR)?
Constructing the HTML for
your view on the
server-side of the web
request.
Client-Side
Rendered
Server-Side Rendered
JavaScript
loads
Hydration
Why server-side
render?
‣ Performance
‣ Search engine optimization
‣ Site works without JavaScript
React has built-in support for SSR
with ReactDOMServer
Render a React App
(Server Side)
‣ Running on the server,
ReactDOMServer.renderToString()
will return a string of HTML
‣ Running on the client,
ReactDOM.hydrate() will
attach the event listeners, and
pick up subsequent rendering
client-side
// Shared
const App = <h1>Hello, world</h1>;
// Server side
ReactDOMServer.renderToString(App);
// Client side
ReactDOM.hydrate(App, root);
Universal JavaScript: The same application
code (components) is run on both client and
server.
(sometimes also referred to as Isomorphic JavaScript)
For universal JavaScript we need a way to
execute JavaScript on the server.
Let’s look at a few diïŹ€erent possible
architectures.
1. V8Js → running JavaScript from PHP
2. Node.js → requests to a standalone JS service
a. Web requests go to PHP, which then makes requests to
Node.js service for HTML
b. Web requests go to Node.js, which then makes requests to
PHP for data
SSR with V8Js extension in PHP
What is V8Js?
A PHP extension which embeds the
V8 JavaScript engine
A PHP extension which embeds the
V8 JavaScript engine
1. Install V8Js
○ Try the V8Js Docker
image or a pre-built
binary
○ Or compile latest version
yourself
2. Enable the extension in php
(extension=v8js.so)
Success!
Execute JS in PHP
‣ With V8js, JS can be executed
from PHP
‣ From this starting point, we
could build a PHP class to
consume JS modules, and
output the result as HTML
<?php
$v8 = new V8Js();
$js = "const name = 'World';";
$js .= "const greeting = 'Hello';";
$js .= "function printGreeting(str) {";
$js .= " return greeting + ‘, ' + str + '!';";
$js .= "}";
$js .= "printGreeting(name);";
echo($v8->executeString($js));
Using the V8Js Extension
+ No additional service calls
need to be made
- Builds can be diïŹ€icult to
maintain
- No built-in Node.js libraries
or tooling available
Client
V8js
SSR with requests to Node.js from PHP
What is Node.js?
A JavaScript runtime built on the
V8 engine.
A JavaScript runtime built on the
V8 engine.
1. Install node.js as a standalone
service; can be on same host,
or another.
2. Your web host may already
support it
○ See oïŹ€icial Docker images
○ Or install yourself
PHP requests to Node.js
+ Full Node.js support
+ PHP can still handle routing,
and partial view rendering
- Additional service to manage
Client
Hypernova: a Node.js service for
server-side rendering JavaScript views
Hypernova
‣ Airbnb open sourced a standalone
Node.js service for rendering React
components: airbnb/hypernova
‣ Wayfair open sourced a PHP client for
Hypernova: wayfair/hypernova-php
SSR with in Node.js with data requests to PHP
Node.js requests to PHP
+ Full Node.js support
+ Both views and routes live in
Node.js
- May be diïŹ€icult to
incrementally migrate to
- PHP is essentially just a data
access layer
Client
Next.js: SSR framework for React.js
‣ Next.js is a complete framework for
server-side rendered react in Node.js,
with out-of-the-box support for features
like routing, code splitting, caching, and
data fetching.
Future of React.js and SSR
JS
Loads
Hydrate all at onceStreaming Server Side Rendering
React now supports streaming using ReactDOMServer.renderToNodeStream() .
We can use HTML Chunked Encoding to flush content as its rendered ready
(e.g., PHP’s ob_flush() ).
Streaming SSR
Load JS incrementally for progressive hydration
Streaming Server Side Rendering
Streaming SSR w/ Partial Hydration
Continued Investment in
React.js Server-Side
Rendering
Takeaways
Easiest way to get started with
React.js is 100% client-side
rendering
React.js has solid server-side
rendering support
Think about how you’re
architecting the view layer of
your application
React.js + SSR can help make
the view layer a first class piece
of your web architecture
Give it a try!
Dank je wel!
Andrew Rota
@AndrewRota

Weitere Àhnliche Inhalte

Was ist angesagt?

Introduction to GraphQL
Introduction to GraphQLIntroduction to GraphQL
Introduction to GraphQLRodrigo Prates
 
Spring Boot+Kafka: the New Enterprise Platform
Spring Boot+Kafka: the New Enterprise PlatformSpring Boot+Kafka: the New Enterprise Platform
Spring Boot+Kafka: the New Enterprise PlatformVMware Tanzu
 
MySQL Audit æ©Ÿćˆ¶æ‡‰ç”šæŠ€èĄ“
MySQL Audit æ©Ÿćˆ¶æ‡‰ç”šæŠ€èĄ“MySQL Audit æ©Ÿćˆ¶æ‡‰ç”šæŠ€èĄ“
MySQL Audit æ©Ÿćˆ¶æ‡‰ç”šæŠ€èĄ“Jamie Lee
 
Event Driven Systems with Spring Boot, Spring Cloud Streams and Kafka
Event Driven Systems with Spring Boot, Spring Cloud Streams and KafkaEvent Driven Systems with Spring Boot, Spring Cloud Streams and Kafka
Event Driven Systems with Spring Boot, Spring Cloud Streams and KafkaVMware Tanzu
 
Introduction to Spring WebFlux #jsug #sf_a1
Introduction to Spring WebFlux #jsug #sf_a1Introduction to Spring WebFlux #jsug #sf_a1
Introduction to Spring WebFlux #jsug #sf_a1Toshiaki Maki
 
Build RESTful API Using Express JS
Build RESTful API Using Express JSBuild RESTful API Using Express JS
Build RESTful API Using Express JSCakra Danu Sedayu
 
Create a RESTful API with NodeJS, Express and MongoDB
Create a RESTful API with NodeJS, Express and MongoDBCreate a RESTful API with NodeJS, Express and MongoDB
Create a RESTful API with NodeJS, Express and MongoDBHengki Sihombing
 
Wroclaw GraphQL - GraphQL in Java
Wroclaw GraphQL - GraphQL in JavaWroclaw GraphQL - GraphQL in Java
Wroclaw GraphQL - GraphQL in JavaMarcinStachniuk
 
Nodejs Explained with Examples
Nodejs Explained with ExamplesNodejs Explained with Examples
Nodejs Explained with ExamplesGabriele Lana
 
React & GraphQL
React & GraphQLReact & GraphQL
React & GraphQLNikolas Burk
 
[넀읎ëČ„ì˜€í”ˆì†ŒìŠ€ì„žëŻžë‚˜] Pinpointë„Œ 읎용핎서 서ëČ„ëŠŹìŠ€ 플랫폌 Apache Openwhisk 튞레읎싱하Ʞ - 였ìŠč현
[넀읎ëČ„ì˜€í”ˆì†ŒìŠ€ì„žëŻžë‚˜] Pinpointë„Œ 읎용핎서 서ëČ„ëŠŹìŠ€ 플랫폌 Apache Openwhisk 튞레읎싱하Ʞ - 였ìŠč현[넀읎ëČ„ì˜€í”ˆì†ŒìŠ€ì„žëŻžë‚˜] Pinpointë„Œ 읎용핎서 서ëČ„ëŠŹìŠ€ 플랫폌 Apache Openwhisk 튞레읎싱하Ʞ - 였ìŠč현
[넀읎ëČ„ì˜€í”ˆì†ŒìŠ€ì„žëŻžë‚˜] Pinpointë„Œ 읎용핎서 서ëČ„ëŠŹìŠ€ 플랫폌 Apache Openwhisk 튞레읎싱하Ʞ - 였ìŠč현NAVER Engineering
 
Secrets of Performance Tuning Java on Kubernetes
Secrets of Performance Tuning Java on KubernetesSecrets of Performance Tuning Java on Kubernetes
Secrets of Performance Tuning Java on KubernetesBruno Borges
 
NextJS, A JavaScript Framework for building next generation SPA
NextJS, A JavaScript Framework for building next generation SPA  NextJS, A JavaScript Framework for building next generation SPA
NextJS, A JavaScript Framework for building next generation SPA Pramendra Gupta
 
introduction to Vue.js 3
introduction to Vue.js 3 introduction to Vue.js 3
introduction to Vue.js 3 ArezooKmn
 
Web Server Technologies I: HTTP & Getting Started
Web Server Technologies I: HTTP & Getting StartedWeb Server Technologies I: HTTP & Getting Started
Web Server Technologies I: HTTP & Getting StartedPort80 Software
 

Was ist angesagt? (20)

Introduction to GraphQL
Introduction to GraphQLIntroduction to GraphQL
Introduction to GraphQL
 
Spring Boot+Kafka: the New Enterprise Platform
Spring Boot+Kafka: the New Enterprise PlatformSpring Boot+Kafka: the New Enterprise Platform
Spring Boot+Kafka: the New Enterprise Platform
 
MySQL Audit æ©Ÿćˆ¶æ‡‰ç”šæŠ€èĄ“
MySQL Audit æ©Ÿćˆ¶æ‡‰ç”šæŠ€èĄ“MySQL Audit æ©Ÿćˆ¶æ‡‰ç”šæŠ€èĄ“
MySQL Audit æ©Ÿćˆ¶æ‡‰ç”šæŠ€èĄ“
 
Springboot Microservices
Springboot MicroservicesSpringboot Microservices
Springboot Microservices
 
Event Driven Systems with Spring Boot, Spring Cloud Streams and Kafka
Event Driven Systems with Spring Boot, Spring Cloud Streams and KafkaEvent Driven Systems with Spring Boot, Spring Cloud Streams and Kafka
Event Driven Systems with Spring Boot, Spring Cloud Streams and Kafka
 
Introduction to Spring WebFlux #jsug #sf_a1
Introduction to Spring WebFlux #jsug #sf_a1Introduction to Spring WebFlux #jsug #sf_a1
Introduction to Spring WebFlux #jsug #sf_a1
 
Build RESTful API Using Express JS
Build RESTful API Using Express JSBuild RESTful API Using Express JS
Build RESTful API Using Express JS
 
GraphQL
GraphQLGraphQL
GraphQL
 
Reactjs
Reactjs Reactjs
Reactjs
 
Create a RESTful API with NodeJS, Express and MongoDB
Create a RESTful API with NodeJS, Express and MongoDBCreate a RESTful API with NodeJS, Express and MongoDB
Create a RESTful API with NodeJS, Express and MongoDB
 
Wroclaw GraphQL - GraphQL in Java
Wroclaw GraphQL - GraphQL in JavaWroclaw GraphQL - GraphQL in Java
Wroclaw GraphQL - GraphQL in Java
 
Nodejs Explained with Examples
Nodejs Explained with ExamplesNodejs Explained with Examples
Nodejs Explained with Examples
 
React & GraphQL
React & GraphQLReact & GraphQL
React & GraphQL
 
[넀읎ëČ„ì˜€í”ˆì†ŒìŠ€ì„žëŻžë‚˜] Pinpointë„Œ 읎용핎서 서ëČ„ëŠŹìŠ€ 플랫폌 Apache Openwhisk 튞레읎싱하Ʞ - 였ìŠč현
[넀읎ëČ„ì˜€í”ˆì†ŒìŠ€ì„žëŻžë‚˜] Pinpointë„Œ 읎용핎서 서ëČ„ëŠŹìŠ€ 플랫폌 Apache Openwhisk 튞레읎싱하Ʞ - 였ìŠč현[넀읎ëČ„ì˜€í”ˆì†ŒìŠ€ì„žëŻžë‚˜] Pinpointë„Œ 읎용핎서 서ëČ„ëŠŹìŠ€ 플랫폌 Apache Openwhisk 튞레읎싱하Ʞ - 였ìŠč현
[넀읎ëČ„ì˜€í”ˆì†ŒìŠ€ì„žëŻžë‚˜] Pinpointë„Œ 읎용핎서 서ëČ„ëŠŹìŠ€ 플랫폌 Apache Openwhisk 튞레읎싱하Ʞ - 였ìŠč현
 
Secrets of Performance Tuning Java on Kubernetes
Secrets of Performance Tuning Java on KubernetesSecrets of Performance Tuning Java on Kubernetes
Secrets of Performance Tuning Java on Kubernetes
 
NextJS, A JavaScript Framework for building next generation SPA
NextJS, A JavaScript Framework for building next generation SPA  NextJS, A JavaScript Framework for building next generation SPA
NextJS, A JavaScript Framework for building next generation SPA
 
Universal React apps in Next.js
Universal React apps in Next.jsUniversal React apps in Next.js
Universal React apps in Next.js
 
introduction to Vue.js 3
introduction to Vue.js 3 introduction to Vue.js 3
introduction to Vue.js 3
 
RESTful Web Services
RESTful Web ServicesRESTful Web Services
RESTful Web Services
 
Web Server Technologies I: HTTP & Getting Started
Web Server Technologies I: HTTP & Getting StartedWeb Server Technologies I: HTTP & Getting Started
Web Server Technologies I: HTTP & Getting Started
 

Ähnlich wie Integrating React.js Into a PHP Application: Dutch PHP 2019

Isomorphic JavaScript: #DevBeat Master Class
Isomorphic JavaScript: #DevBeat Master ClassIsomorphic JavaScript: #DevBeat Master Class
Isomorphic JavaScript: #DevBeat Master ClassSpike Brehm
 
Is Vue catching up with React.pdf
Is Vue catching up with React.pdfIs Vue catching up with React.pdf
Is Vue catching up with React.pdfMindfire LLC
 
React Js vs Node Js_ Which Framework to Choose for Your Next Web Application
React Js vs Node Js_ Which Framework to Choose for Your Next Web ApplicationReact Js vs Node Js_ Which Framework to Choose for Your Next Web Application
React Js vs Node Js_ Which Framework to Choose for Your Next Web Applicationadityakumar2080
 
GDSC NITS Presents Kickstart into ReactJS
GDSC NITS Presents Kickstart into ReactJSGDSC NITS Presents Kickstart into ReactJS
GDSC NITS Presents Kickstart into ReactJSPratik Majumdar
 
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
 
Web summit.pptx
Web summit.pptxWeb summit.pptx
Web summit.pptx171SagnikRoy
 
Day in a life of a node.js developer
Day in a life of a node.js developerDay in a life of a node.js developer
Day in a life of a node.js developerEdureka!
 
Day In A Life Of A Node.js Developer
Day In A Life Of A Node.js DeveloperDay In A Life Of A Node.js Developer
Day In A Life Of A Node.js DeveloperEdureka!
 
Nodejs Intro - Part2 Introduction to Web Applications
Nodejs Intro - Part2 Introduction to Web ApplicationsNodejs Intro - Part2 Introduction to Web Applications
Nodejs Intro - Part2 Introduction to Web ApplicationsBudh Ram Gurung
 
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
 
Review on React JS
Review on React JSReview on React JS
Review on React JSijtsrd
 
React, Flux and more (p1)
React, Flux and more (p1)React, Flux and more (p1)
React, Flux and more (p1)tuanpa206
 
Reactjs notes.pptx for web development- tutorial and theory
Reactjs  notes.pptx for web development- tutorial and theoryReactjs  notes.pptx for web development- tutorial and theory
Reactjs notes.pptx for web development- tutorial and theoryjobinThomas54
 
Combining react with node js to develop successful full stack web applications
Combining react with node js to develop successful full stack web applicationsCombining react with node js to develop successful full stack web applications
Combining react with node js to develop successful full stack web applicationsKaty Slemon
 

Ähnlich wie Integrating React.js Into a PHP Application: Dutch PHP 2019 (20)

Reactjs Basics
Reactjs BasicsReactjs Basics
Reactjs Basics
 
Isomorphic JavaScript: #DevBeat Master Class
Isomorphic JavaScript: #DevBeat Master ClassIsomorphic JavaScript: #DevBeat Master Class
Isomorphic JavaScript: #DevBeat Master Class
 
Is Vue catching up with React.pdf
Is Vue catching up with React.pdfIs Vue catching up with React.pdf
Is Vue catching up with React.pdf
 
React Js vs Node Js_ Which Framework to Choose for Your Next Web Application
React Js vs Node Js_ Which Framework to Choose for Your Next Web ApplicationReact Js vs Node Js_ Which Framework to Choose for Your Next Web Application
React Js vs Node Js_ Which Framework to Choose for Your Next Web Application
 
GDSC NITS Presents Kickstart into ReactJS
GDSC NITS Presents Kickstart into ReactJSGDSC NITS Presents Kickstart into ReactJS
GDSC NITS Presents Kickstart into ReactJS
 
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]
 
Web summit.pptx
Web summit.pptxWeb summit.pptx
Web summit.pptx
 
Node js
Node jsNode js
Node js
 
AFTAB AHMED.pptx
AFTAB AHMED.pptxAFTAB AHMED.pptx
AFTAB AHMED.pptx
 
Nodejs vs react js converted
Nodejs vs react js convertedNodejs vs react js converted
Nodejs vs react js converted
 
Nodejs
NodejsNodejs
Nodejs
 
Day in a life of a node.js developer
Day in a life of a node.js developerDay in a life of a node.js developer
Day in a life of a node.js developer
 
Day In A Life Of A Node.js Developer
Day In A Life Of A Node.js DeveloperDay In A Life Of A Node.js Developer
Day In A Life Of A Node.js Developer
 
Nodejs Intro - Part2 Introduction to Web Applications
Nodejs Intro - Part2 Introduction to Web ApplicationsNodejs Intro - Part2 Introduction to Web Applications
Nodejs Intro - Part2 Introduction to Web Applications
 
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)
 
Review on React JS
Review on React JSReview on React JS
Review on React JS
 
React, Flux and more (p1)
React, Flux and more (p1)React, Flux and more (p1)
React, Flux and more (p1)
 
Reactjs notes.pptx for web development- tutorial and theory
Reactjs  notes.pptx for web development- tutorial and theoryReactjs  notes.pptx for web development- tutorial and theory
Reactjs notes.pptx for web development- tutorial and theory
 
Combining react with node js to develop successful full stack web applications
Combining react with node js to develop successful full stack web applicationsCombining react with node js to develop successful full stack web applications
Combining react with node js to develop successful full stack web applications
 

Mehr von Andrew Rota

Performant APIs with GraphQL and PHP (Dutch PHP 2019)
Performant APIs with GraphQL and PHP (Dutch PHP 2019)Performant APIs with GraphQL and PHP (Dutch PHP 2019)
Performant APIs with GraphQL and PHP (Dutch PHP 2019)Andrew Rota
 
Getting Started with GraphQL && PHP
Getting Started with GraphQL && PHPGetting Started with GraphQL && PHP
Getting Started with GraphQL && PHPAndrew Rota
 
Tutorial: Building a GraphQL API in PHP
Tutorial: Building a GraphQL API in PHPTutorial: Building a GraphQL API in PHP
Tutorial: Building a GraphQL API in PHPAndrew Rota
 
Building a GraphQL API in PHP
Building a GraphQL API in PHPBuilding a GraphQL API in PHP
Building a GraphQL API in PHPAndrew Rota
 
Ten practical ways to improve front-end performance
Ten practical ways to improve front-end performanceTen practical ways to improve front-end performance
Ten practical ways to improve front-end performanceAndrew Rota
 
Component Based UI Architectures for the Web
Component Based UI Architectures for the WebComponent Based UI Architectures for the Web
Component Based UI Architectures for the WebAndrew Rota
 
Client-Side Performance Monitoring (MobileTea, Rome)
Client-Side Performance Monitoring (MobileTea, Rome)Client-Side Performance Monitoring (MobileTea, Rome)
Client-Side Performance Monitoring (MobileTea, Rome)Andrew Rota
 
Effectively Monitoring Client-Side Performance
Effectively Monitoring Client-Side PerformanceEffectively Monitoring Client-Side Performance
Effectively Monitoring Client-Side PerformanceAndrew Rota
 
UI Rendering at Wayfair
UI Rendering at WayfairUI Rendering at Wayfair
UI Rendering at WayfairAndrew Rota
 
Better PHP-Frontend Integration with Tungsten.js
Better PHP-Frontend Integration with Tungsten.jsBetter PHP-Frontend Integration with Tungsten.js
Better PHP-Frontend Integration with Tungsten.jsAndrew Rota
 
Tungsten.js: Building a Modular Framework
Tungsten.js: Building a Modular FrameworkTungsten.js: Building a Modular Framework
Tungsten.js: Building a Modular FrameworkAndrew Rota
 
Why Static Type Checking is Better
Why Static Type Checking is BetterWhy Static Type Checking is Better
Why Static Type Checking is BetterAndrew Rota
 
An Exploration of Frameworks – and Why We Built Our Own
An Exploration of Frameworks – and Why We Built Our OwnAn Exploration of Frameworks – and Why We Built Our Own
An Exploration of Frameworks – and Why We Built Our OwnAndrew Rota
 
The Complementarity of React and Web Components
The Complementarity of React and Web ComponentsThe Complementarity of React and Web Components
The Complementarity of React and Web ComponentsAndrew Rota
 
Web Components + Backbone: a Game-Changing Combination
Web Components + Backbone: a Game-Changing CombinationWeb Components + Backbone: a Game-Changing Combination
Web Components + Backbone: a Game-Changing CombinationAndrew Rota
 
Bem methodology
Bem methodologyBem methodology
Bem methodologyAndrew Rota
 
Web Components and Modular CSS
Web Components and Modular CSSWeb Components and Modular CSS
Web Components and Modular CSSAndrew Rota
 

Mehr von Andrew Rota (17)

Performant APIs with GraphQL and PHP (Dutch PHP 2019)
Performant APIs with GraphQL and PHP (Dutch PHP 2019)Performant APIs with GraphQL and PHP (Dutch PHP 2019)
Performant APIs with GraphQL and PHP (Dutch PHP 2019)
 
Getting Started with GraphQL && PHP
Getting Started with GraphQL && PHPGetting Started with GraphQL && PHP
Getting Started with GraphQL && PHP
 
Tutorial: Building a GraphQL API in PHP
Tutorial: Building a GraphQL API in PHPTutorial: Building a GraphQL API in PHP
Tutorial: Building a GraphQL API in PHP
 
Building a GraphQL API in PHP
Building a GraphQL API in PHPBuilding a GraphQL API in PHP
Building a GraphQL API in PHP
 
Ten practical ways to improve front-end performance
Ten practical ways to improve front-end performanceTen practical ways to improve front-end performance
Ten practical ways to improve front-end performance
 
Component Based UI Architectures for the Web
Component Based UI Architectures for the WebComponent Based UI Architectures for the Web
Component Based UI Architectures for the Web
 
Client-Side Performance Monitoring (MobileTea, Rome)
Client-Side Performance Monitoring (MobileTea, Rome)Client-Side Performance Monitoring (MobileTea, Rome)
Client-Side Performance Monitoring (MobileTea, Rome)
 
Effectively Monitoring Client-Side Performance
Effectively Monitoring Client-Side PerformanceEffectively Monitoring Client-Side Performance
Effectively Monitoring Client-Side Performance
 
UI Rendering at Wayfair
UI Rendering at WayfairUI Rendering at Wayfair
UI Rendering at Wayfair
 
Better PHP-Frontend Integration with Tungsten.js
Better PHP-Frontend Integration with Tungsten.jsBetter PHP-Frontend Integration with Tungsten.js
Better PHP-Frontend Integration with Tungsten.js
 
Tungsten.js: Building a Modular Framework
Tungsten.js: Building a Modular FrameworkTungsten.js: Building a Modular Framework
Tungsten.js: Building a Modular Framework
 
Why Static Type Checking is Better
Why Static Type Checking is BetterWhy Static Type Checking is Better
Why Static Type Checking is Better
 
An Exploration of Frameworks – and Why We Built Our Own
An Exploration of Frameworks – and Why We Built Our OwnAn Exploration of Frameworks – and Why We Built Our Own
An Exploration of Frameworks – and Why We Built Our Own
 
The Complementarity of React and Web Components
The Complementarity of React and Web ComponentsThe Complementarity of React and Web Components
The Complementarity of React and Web Components
 
Web Components + Backbone: a Game-Changing Combination
Web Components + Backbone: a Game-Changing CombinationWeb Components + Backbone: a Game-Changing Combination
Web Components + Backbone: a Game-Changing Combination
 
Bem methodology
Bem methodologyBem methodology
Bem methodology
 
Web Components and Modular CSS
Web Components and Modular CSSWeb Components and Modular CSS
Web Components and Modular CSS
 

KĂŒrzlich hochgeladen

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...software pro Development
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...kalichargn70th171
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
call girls in Vaishali (Ghaziabad) 🔝 >àŒ’8448380779 🔝 genuine Escort Service đŸ”âœ”ïžâœ”ïž
call girls in Vaishali (Ghaziabad) 🔝 >àŒ’8448380779 🔝 genuine Escort Service đŸ”âœ”ïžâœ”ïžcall girls in Vaishali (Ghaziabad) 🔝 >àŒ’8448380779 🔝 genuine Escort Service đŸ”âœ”ïžâœ”ïž
call girls in Vaishali (Ghaziabad) 🔝 >àŒ’8448380779 🔝 genuine Escort Service đŸ”âœ”ïžâœ”ïžDelhi Call girls
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 

KĂŒrzlich hochgeladen (20)

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Vip Call Girls Noida âžĄïž Delhi âžĄïž 9999965857 No Advance 24HRS Live
Vip Call Girls Noida âžĄïž Delhi âžĄïž 9999965857 No Advance 24HRS LiveVip Call Girls Noida âžĄïž Delhi âžĄïž 9999965857 No Advance 24HRS Live
Vip Call Girls Noida âžĄïž Delhi âžĄïž 9999965857 No Advance 24HRS Live
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
call girls in Vaishali (Ghaziabad) 🔝 >àŒ’8448380779 🔝 genuine Escort Service đŸ”âœ”ïžâœ”ïž
call girls in Vaishali (Ghaziabad) 🔝 >àŒ’8448380779 🔝 genuine Escort Service đŸ”âœ”ïžâœ”ïžcall girls in Vaishali (Ghaziabad) 🔝 >àŒ’8448380779 🔝 genuine Escort Service đŸ”âœ”ïžâœ”ïž
call girls in Vaishali (Ghaziabad) 🔝 >àŒ’8448380779 🔝 genuine Escort Service đŸ”âœ”ïžâœ”ïž
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 

Integrating React.js Into a PHP Application: Dutch PHP 2019

  • 1. Integrating React.js Into a PHP Application Slides online at: @AndrewRota | Dutch PHP Conference 2019
  • 2. What is React.js? “A JavaScript library for building user interfaces” https://reactjs.org/
  • 3. React.js has, by far, the greatest market share of any frontend framework Laurie Voss, npm and the future of JavaScript (2018)
  • 4. ...and it’s still growing Laurie Voss, npm and the future of JavaScript (2018)
  • 5. Among developers, use of both PHP and React.js are correlated Stack Overflow, Developer Survey Results 2019
  • 6. As developers, as want to build the best interfaces for our users, and React is arguably one of the best tools for building modern web UIs.
  • 8. Agenda ● ⚛ Lightning Introduction to React.js ● 🎹 Getting Started with Client-Side Rendered React ● ⚙ Server-Side Rendering Architectures ■ V8Js PHP Extension ■ PHP Requests to a Node.js Service ■ Node.js Requests to PHP ● ✹ Future of React.js SSR ● 💡Takeaways
  • 9. What can React.js add to a PHP web application?
  • 10. How can we integrate React.js into a PHP web application?
  • 11. PHP and React.js can complement each other in a web application
  • 12. Make views a first-class aspect of your web application
  • 15. Flexibility to support “single-page application” experiences
  • 16. Frontend frameworks can unlock new interaction patterns
  • 17. React.js makes it easy (and fun) to create and manage rich view logic
  • 18. What is React.js? “A JavaScript library for building user interfaces”
  • 19. Declarative ‣ Design views as “components” which accept props and return React elements ‣ React will handle rendering and re-rendering the DOM when data changes function Hello(props) { return <h1>Hello, {props.name}</h1>; }
  • 20. Composable ‣ In addition to DOM nodes, components can also render other components ‣ You can also render child components for more generic “box” components using props.children. function Hello(props) { return <h2>My name is, {props.name}</h2>; } function NameBadge(props) { return (<div> Welcome to {props.conf} Conference.<br /> {props.children} </div>) } function App() { return (<div> <NameBadge conf={'Dutch PHP 2019'}> <Hello name={'Andrew'} /> </NameBadge> </div>) }
  • 21. Encapsulate State ‣ Components can manage their own encapsulated state ‣ When state is shared across components, a common pattern is to lift that state up to a common ancestor ‣ Libraries such as Redux or MobX can help with more complex state management import {Component} from "react"; class App extends Component { state = {count: 0}; handleClick = () => { this.setState(state => { return {count: state.count + 1} }); }; render() { return <div> <span>Count: {this.state.count} </span> <button onClick={this.handleClick}>+</button> </div>; } }
  • 22. Adding React.js to your PHP site: the easy way
 100% Client-Side Rendering
  • 23. Render a React App ‣ Start with the root element on a page, and use ReactDOM.render to start the application const root = document.getElementById('root'); const App = <h1>Hello, world</h1>; ReactDOM.render(App, root);
  • 24. Initial page load is blank JavaScript loads Client-Side Rendered
  • 25. Incremental Adoption ‣ A 100% react application would have a single react root. ‣ Use ReactDOM.render() to create multiple roots when converting an application ‣ In general, convert components from the “bottom up” of the view tree
  • 26. But we’ve only partially integrated React.js into our site... Enter Server-Side Rendering
  • 27. What is server-side rendering (SSR)? Constructing the HTML for your view on the server-side of the web request.
  • 29. Why server-side render? ‣ Performance ‣ Search engine optimization ‣ Site works without JavaScript
  • 30. React has built-in support for SSR with ReactDOMServer
  • 31. Render a React App (Server Side) ‣ Running on the server, ReactDOMServer.renderToString() will return a string of HTML ‣ Running on the client, ReactDOM.hydrate() will attach the event listeners, and pick up subsequent rendering client-side // Shared const App = <h1>Hello, world</h1>; // Server side ReactDOMServer.renderToString(App); // Client side ReactDOM.hydrate(App, root);
  • 32. Universal JavaScript: The same application code (components) is run on both client and server. (sometimes also referred to as Isomorphic JavaScript)
  • 33. For universal JavaScript we need a way to execute JavaScript on the server.
  • 34. Let’s look at a few diïŹ€erent possible architectures.
  • 35. 1. V8Js → running JavaScript from PHP 2. Node.js → requests to a standalone JS service a. Web requests go to PHP, which then makes requests to Node.js service for HTML b. Web requests go to Node.js, which then makes requests to PHP for data
  • 36. SSR with V8Js extension in PHP
  • 37. What is V8Js? A PHP extension which embeds the V8 JavaScript engine
  • 38. A PHP extension which embeds the V8 JavaScript engine 1. Install V8Js ○ Try the V8Js Docker image or a pre-built binary ○ Or compile latest version yourself 2. Enable the extension in php (extension=v8js.so)
  • 40. Execute JS in PHP ‣ With V8js, JS can be executed from PHP ‣ From this starting point, we could build a PHP class to consume JS modules, and output the result as HTML <?php $v8 = new V8Js(); $js = "const name = 'World';"; $js .= "const greeting = 'Hello';"; $js .= "function printGreeting(str) {"; $js .= " return greeting + ‘, ' + str + '!';"; $js .= "}"; $js .= "printGreeting(name);"; echo($v8->executeString($js));
  • 41. Using the V8Js Extension + No additional service calls need to be made - Builds can be diïŹ€icult to maintain - No built-in Node.js libraries or tooling available Client V8js
  • 42. SSR with requests to Node.js from PHP
  • 43. What is Node.js? A JavaScript runtime built on the V8 engine.
  • 44. A JavaScript runtime built on the V8 engine. 1. Install node.js as a standalone service; can be on same host, or another. 2. Your web host may already support it ○ See oïŹ€icial Docker images ○ Or install yourself
  • 45. PHP requests to Node.js + Full Node.js support + PHP can still handle routing, and partial view rendering - Additional service to manage Client
  • 46. Hypernova: a Node.js service for server-side rendering JavaScript views Hypernova ‣ Airbnb open sourced a standalone Node.js service for rendering React components: airbnb/hypernova ‣ Wayfair open sourced a PHP client for Hypernova: wayfair/hypernova-php
  • 47. SSR with in Node.js with data requests to PHP
  • 48. Node.js requests to PHP + Full Node.js support + Both views and routes live in Node.js - May be diïŹ€icult to incrementally migrate to - PHP is essentially just a data access layer Client
  • 49. Next.js: SSR framework for React.js ‣ Next.js is a complete framework for server-side rendered react in Node.js, with out-of-the-box support for features like routing, code splitting, caching, and data fetching.
  • 51. JS Loads Hydrate all at onceStreaming Server Side Rendering React now supports streaming using ReactDOMServer.renderToNodeStream() . We can use HTML Chunked Encoding to flush content as its rendered ready (e.g., PHP’s ob_flush() ). Streaming SSR
  • 52. Load JS incrementally for progressive hydration Streaming Server Side Rendering Streaming SSR w/ Partial Hydration
  • 53. Continued Investment in React.js Server-Side Rendering
  • 55. Easiest way to get started with React.js is 100% client-side rendering
  • 56. React.js has solid server-side rendering support
  • 57. Think about how you’re architecting the view layer of your application
  • 58. React.js + SSR can help make the view layer a first class piece of your web architecture
  • 59. Give it a try!
  • 60. Dank je wel! Andrew Rota @AndrewRota