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
Demystifying React for Symfony developers
Search
Titouan Galopin
November 22, 2019
Programming
3
760
Demystifying React for Symfony developers
SymfonyCon Amsterdam 2019
Titouan Galopin
November 22, 2019
Tweet
Share
More Decks by Titouan Galopin
See All by Titouan Galopin
Concevoir son API pour le futur
tgalopin
2
1.5k
Content editing in Symfony
tgalopin
3
970
Symfony UX: a new JavaScript ecosystem for Symfony
tgalopin
4
4k
[SymfonyLive Paris 2020] Pragmatic frontend for Symfony developers
tgalopin
2
1.1k
SymfonyInsight: How to setup quality processes in Symfony apps
tgalopin
2
370
Symfony 5 - AFUP Day Lille 2020
tgalopin
1
860
Pragmatic frontend for Symfony developers
tgalopin
2
1.1k
Symfony 5
tgalopin
1
680
Plongée dans l'injection de dépendances de Symfony
tgalopin
3
1.2k
Other Decks in Programming
See All in Programming
Kaigi on Railsに初参加したら、その日にLT登壇が決定した件について
tama50505
0
100
数十万行のプロジェクトを Scala 2から3に完全移行した
xuwei_k
0
280
ゆるやかにgolangci-lintのルールを強くする / Kyoto.go #56
utgwkk
2
390
Security_for_introducing_eBPF
kentatada
0
110
Fibonacci Function Gallery - Part 1
philipschwarz
PRO
0
220
Beyond ORM
77web
7
910
わたしの星のままで一番星になる ~ 出産を機にSIerからEC事業会社に転職した話 ~
kimura_m_29
0
180
17年周年のWebアプリケーションにTanStack Queryを導入する / Implementing TanStack Query in a 17th Anniversary Web Application
saitolume
0
250
短期間での新規プロダクト開発における「コスパの良い」Goのテスト戦略」 / kamakura.go
n3xem
2
170
各クラウドサービスにおける.NETの対応と見解
ymd65536
0
110
testcontainers のススメ
sgash708
1
120
PHPで作るWebSocketサーバー ~リアクティブなアプリケーションを知るために~ / WebSocket Server in PHP - To know reactive applications
seike460
PRO
2
470
Featured
See All Featured
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
2k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Adopting Sorbet at Scale
ufuk
73
9.1k
Building an army of robots
kneath
302
44k
A Tale of Four Properties
chriscoyier
157
23k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
5
450
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
The World Runs on Bad Software
bkeepers
PRO
65
11k
Transcript
Demystifying React for Symfony developers Titouan GALOPIN
2 Titouan Galopin Product Manager SymfonyInsight insight.symfony.com
Agenda 1. What’s React? 2. Components 3. Symfony integration with
Webpack Encore 3
4 1. What’s React?
5 How should we structure Javascript applications?
6 AngularJS Vue Backbone.js Dart Ember.js Dojo Meteor ...
7 There is a consensus here: MVC, or MVVM, or
MVW, ...
8 Models
9 { firstName: 'Titouan', lastName: 'Galopin', email: '
[email protected]
' }
10 Models = Observable objects
11 Bidirectional data-binding An change in the model updates the
view An change in the view updates the model
12 Two data sources Model View
13 This encourages mutations
14 { firstName: 'Tito', lastName: 'Galopin', email: '
[email protected]
' } Model
{ firstName: 'Titouan''Tito', lastName: 'Galopin', email: '
[email protected]
' } View { firstName: 'Tito' } Mutation
15 But mutations are complex
16 Complex to apply Complex to reproduce Complex to debug
17 What if we could do better?
18 The simplest way to build views is to avoid
mutations altogether
19 React does not use mutations at all
20 Instead, it has a single data source
21 Model View Observable model Two-way data flow
22 Model Observable model Two-way data flow View React One-way
data flow Model Dispatcher View
View 23 Model Observable model Two-way data flow View React
One-way data flow Model Dispatcher View
24 Model Observable model Two-way data flow View React One-way
data flow Model Dispatcher View
25 But isn’t that super slow? What about user inputs?
26 We want the benefits to blow the view away
and re-render it ... … while dealing properly with every edge cases
27 That’s React
28 React is a Javascript library for building User Interfaces
29 React is declarative render(data: array) => view: string
30 React re-renders all your view when data changes
31 2. Components
32 React’s main concept is Components
33 A component = A description of a part of
your UI that depends on your current model
34 It’s more than a template though
35 Store Dispatcher View Each component has its own model
and behavior This is component
36 The aim? Abstract away the implementation details of parts
of the UI
37
38 <Autocomplete source="/api/users" onSelect={this.handleUserSelected} />
39 <Autocomplete source="/api/users" onSelect={this.handleUserSelected} /> Property
40 But how does it work?
41 A component = A state + A way to
display that state
42 Store (Model) Dispatcher View React handles the Dispatcher and
the Store You only implement the view and define the structure of the Store
43 The state is a single Javascript object
44 The view is described by a single render() function
45 Store (state) Initial state
46 render Store (state) View (components) Initial state
47
48 class Autocomplete extends Component { constructor() { this.state =
{ results: [] }; } }
49 class Autocomplete extends Component { // ... render() {
return ( <ul> {this.state.results.map(function(result) { return ( <li>{result.name}</li> ); })} </ul> ); } }
50 class Autocomplete extends Component { // ... render() {
return ( <ul> {this.state.results.map(function(result) { return ( <li>{result.name}</li> ); })} </ul> ); } } Use of the state
51 class Autocomplete extends Component { // ... render() {
return ( <ul> {this.state.results.map(function(result) { return ( <li>{result.name}</li> ); })} </ul> ); } } Use of the state To use a property: this.props.source
State = Local data that will change over time Props
= Non-local data, read-only for the component 52
If either the state or the properties change, the component
will be re-rendered 53
54 class Autocomplete extends Component { // ... render() {
return ( <ul> {this.state.results.map(function(result) { return ( <li>{result.name}</li> ); })} </ul> ); } }
55 class Autocomplete extends Component { // ... render() {
return ( <ul> {this.state.results.map(function(result) { return ( <li>{result.name}</li> ); })} </ul> ); } } What’s that?!
56 JSX is a formatting language used by React to
express a representation of views
57 It is compiled to Javascript <li className="hello"> becomes React.DOM.li({
className: 'hello'})
58 class Autocomplete extends Component { // ... render() {
return ( <ul> {this.state.results.map(function(result) { return ( <li>{result.name}</li> ); })} </ul> ); } } Not real HTML tags but components
59 render() returns a tree of components
60 You usually create your own tree of components RegistrationForm
CityAutocompleteInput PhoneNumberInput HTMLTextInput HTMLTelInput
61 Now, how to update the view?
62 Instead of updates (mutations) React uses reconciliation
63 setState()
64 React compares the view representations and apply the changes
to the DOM
Tree of components before change Tree of components after change
setState
66 Tree of components before change Tree of components after
change setState render render View before View after
67 Tree of components before change Tree of components after
change setState render render View before View after React compares both and apply only needed changes to the DOM
68 class Autocomplete extends Component { // ... handleChange(event) {
api.fetchResults(event.target.value).then(results => { this.setState({ results: results }) }); } // ... render() { return ( <div> <input type="text" onChange={this.handleChange} /> // ... </div> ); } }
69 setState() merges the current state and the given data
...
70 … then calls recursively render() on the component and
all its children
71 render Store (state) View (components) Initial state
72 Dispatcher (React) setState render Store (state) View (components) Initial
state
73 Dispatcher (React) setState render Handled by React Store (state)
View (components) Initial state
74 Now you know React!
75 3. Symfony integration with Webpack Encore
76 Webpack is a build tool It lets you manipulate
your Javascript and CSS before using it in production (JSX, minification, …)
77 Webpack Encore wraps Webpack around a nice API to
improve its Developer Experience
78 Webpack Encore is awesome to compile React apps to
normal Javascript
79 composer req --dev webpack yarn install https://symfony.com/doc/current/frontend.html
80 yarn add @babel/preset-react react react-dom prop-types
81 // webpack.config.js const Encore = require( '@symfony/webpack-encore' ); Encore
// ... .enableReactPreset(); module.exports = Encore.getWebpackConfig();
82 Once Webpack Encore is ready, you can use it
in Twig to load React
83 // index.html.twig ... <div id="portfolio"></div> … // Output proper
<script src="..."> tags {{ encore_entry_script_tags('app') }}
84 // app.js import ReactDOM from 'react-dom'; import {App} from
'./App.js'; ReactDOM.render( <App />, document.getElementById('portfolio') );
85 // app.js import ReactDOM from 'react-dom'; import {App} from
'./App.js'; ReactDOM.render( <App />, document.getElementById('portfolio') ); Tree of components
86 And that’s all! You can now use React into
Symfony
87 Conclusion
Thanks! 88 For any question: ▪ @titouangalopin on Twitter ▪
titouan.galopin @symfony.com