Slide 1

Slide 1 text

React and Redux with Symfony and Webpack Encore Titouan GALOPIN SymfonyLive Paris 2019

Slide 2

Slide 2 text

2 Titouan Galopin Product Manager SymfonyInsight insight.symfony.com Certified Software engineer

Slide 3

Slide 3 text

Agenda 1. What’s React? 2. Components 3. Webpack Encore 4. Redux 5. Example 3

Slide 4

Slide 4 text

4 1. What’s React?

Slide 5

Slide 5 text

5 How should we structure Javascript applications?

Slide 6

Slide 6 text

6 AngularJS Vue Backbone.js Dart Ember.js Dojo Meteor ...

Slide 7

Slide 7 text

7 There is a consensus here: MVC, or MVVM, or MVW, ...

Slide 8

Slide 8 text

8 Models

Slide 9

Slide 9 text

9 Models = Observable objects

Slide 10

Slide 10 text

10 Bidirectional data-binding An change in the model updates the view An change in the view updates the model

Slide 11

Slide 11 text

11 When a change occurs somewhere, it mutates the other part of the system

Slide 12

Slide 12 text

12 But mutations are complex

Slide 13

Slide 13 text

13 Complex to apply Complex to reproduce Complex to debug

Slide 14

Slide 14 text

14 React aims at simplifying this

Slide 15

Slide 15 text

15 React does not use mutations

Slide 16

Slide 16 text

16 Model Observable model Two-way data flow View

Slide 17

Slide 17 text

17 Model Observable model Two-way data flow View React One-way data flow Store Dispatcher View

Slide 18

Slide 18 text

View 18 Model Observable model Two-way data flow View React One-way data flow Store Dispatcher View

Slide 19

Slide 19 text

19 Model Observable model Two-way data flow View React One-way data flow Store Dispatcher View

Slide 20

Slide 20 text

20 React re-renders all your view when data changes

Slide 21

Slide 21 text

21 But isn’t that super slow? What about user inputs?

Slide 22

Slide 22 text

22 We want the benefits to blow the view away and re-render it ... … while dealing properly with every edge cases

Slide 23

Slide 23 text

23 That’s React

Slide 24

Slide 24 text

24 React is a Javascript library for building User Interfaces

Slide 25

Slide 25 text

25 2. Components

Slide 26

Slide 26 text

26 React’s main concept is Components

Slide 27

Slide 27 text

27 A component = A description of a part of your UI that depends on your current model

Slide 28

Slide 28 text

28 It’s more than a template though

Slide 29

Slide 29 text

29 Store Dispatcher View Each component has its own state and behavior This is component

Slide 30

Slide 30 text

30 The aim? Abstract away the implementation details of parts of the UI

Slide 31

Slide 31 text

31

Slide 32

Slide 32 text

32

Slide 33

Slide 33 text

33 Property

Slide 34

Slide 34 text

34 But how does it work?

Slide 35

Slide 35 text

35 A component = A state + A way to display that state

Slide 36

Slide 36 text

36 Store Dispatcher View React handles the Dispatcher and the Store You only implement the view and define the structure of the Store

Slide 37

Slide 37 text

37 The state is a single Javascript object

Slide 38

Slide 38 text

38 The view is described by a single render() function

Slide 39

Slide 39 text

39 Store (state) Initial state

Slide 40

Slide 40 text

40 render Store (state) View (components) Initial state

Slide 41

Slide 41 text

41

Slide 42

Slide 42 text

42 class Autocomplete extends Component { constructor() { this.state = { results: [] }; } }

Slide 43

Slide 43 text

43 class Autocomplete extends Component { // ... render() { return (
    {this.state.results.map(function(result) { return (
  • {result.name}
  • ); })}
); } }

Slide 44

Slide 44 text

44 class Autocomplete extends Component { // ... render() { return (
    {this.state.results.map(function(result) { return (
  • {result.name}
  • ); })}
); } } Use of the state

Slide 45

Slide 45 text

45 class Autocomplete extends Component { // ... render() { return (
    {this.state.results.map(function(result) { return (
  • {result.name}
  • ); })}
); } } Use of the state To use a property: this.props.source

Slide 46

Slide 46 text

State = Local data that will change over time Props = Non-local data, read-only for the component 46

Slide 47

Slide 47 text

If either the state or the properties change, the component will be re-rendered 47

Slide 48

Slide 48 text

48 class Autocomplete extends Component { // ... render() { return (
    {this.state.results.map(function(result) { return (
  • {result.name}
  • ); })}
); } }

Slide 49

Slide 49 text

49 class Autocomplete extends Component { // ... render() { return (
    {this.state.results.map(function(result) { return (
  • {result.name}
  • ); })}
); } } What’s that?!

Slide 50

Slide 50 text

50 JSX is a formatting language used by React to express a representation of views

Slide 51

Slide 51 text

51 It is compiled to Javascript
  • becomes React.DOM.li({ className: 'hello'})
  • Slide 52

    Slide 52 text

    52 class Autocomplete extends Component { // ... render() { return (
      {this.state.results.map(function(result) { return (
    • {result.name}
    • ); })}
    ); } } Not real HTML tags but components

    Slide 53

    Slide 53 text

    53 render() returns a tree of components

    Slide 54

    Slide 54 text

    54 Now, how to update the view?

    Slide 55

    Slide 55 text

    55 Instead of updates (mutations) React uses reconciliation

    Slide 56

    Slide 56 text

    56 React compares the view representations and apply the changes to the DOM

    Slide 57

    Slide 57 text

    57 setState()

    Slide 58

    Slide 58 text

    58 class Autocomplete extends Component { // ... handleChange(event) { api.fetchResults(event.target.value).then(results => { this.setState({ results: results }) }); } // ... render() { return (
    // ...
    ); } }

    Slide 59

    Slide 59 text

    59 setState() merges the current state and the given data ...

    Slide 60

    Slide 60 text

    60 … then calls recursively render() on the component and all its children

    Slide 61

    Slide 61 text

    61 render Store (state) View (components) Initial state

    Slide 62

    Slide 62 text

    62 Dispatcher (React) setState render Store (state) View (components) Initial state

    Slide 63

    Slide 63 text

    63 Dispatcher (React) setState render Handled by React Store (state) View (components) Initial state

    Slide 64

    Slide 64 text

    64 Now you know React!

    Slide 65

    Slide 65 text

    65 3. Webpack Encore

    Slide 66

    Slide 66 text

    66 Webpack is a build tool It lets you manipulate your Javascript and CSS before using it in production (JSX, minification, …)

    Slide 67

    Slide 67 text

    67 Webpack Encore wraps Webpack around a nice API to improve its Developer Experience

    Slide 68

    Slide 68 text

    68 Webpack Encore is awesome to compile React apps to normal Javascript

    Slide 69

    Slide 69 text

    69 composer req --dev webpack yarn install https://symfony.com/doc/current/frontend.html

    Slide 70

    Slide 70 text

    70 yarn add @babel/preset-react react react-dom prop-types

    Slide 71

    Slide 71 text

    71 // webpack.config.js const Encore = require( '@symfony/webpack-encore' ); Encore // ... .enableReactPreset(); module.exports = Encore.getWebpackConfig();

    Slide 72

    Slide 72 text

    72 // app.js import ReactDOM from 'react-dom'; import {App} from './App.js'; ReactDOM.render( , document.getElementById('portfolio') );

    Slide 73

    Slide 73 text

    73 // app.js import ReactDOM from 'react-dom'; import {App} from './App.js'; ReactDOM.render( , document.getElementById('portfolio') ); Tree of components

    Slide 74

    Slide 74 text

    74 4. Redux

    Slide 75

    Slide 75 text

    75 One component = One state object

    Slide 76

    Slide 76 text

    76 In a real React application, you will soon have dozens of React components

    Slide 77

    Slide 77 text

    77 That’s a lot of states

    Slide 78

    Slide 78 text

    78 How to synchronize them ?

    Slide 79

    Slide 79 text

    79 Redux

    Slide 80

    Slide 80 text

    80 Redux helps you manage a single State for your whole application and use it in your components

    Slide 81

    Slide 81 text

    81 Redux works using Actions and Reducers

    Slide 82

    Slide 82 text

    82 Your components receive data from global state as props

    Slide 83

    Slide 83 text

    83 Your components dispatch actions with custom data

    Slide 84

    Slide 84 text

    84 Reducers transform these actions into changes to the global state

    Slide 85

    Slide 85 text

    85 Global state Initial global state Redux

    Slide 86

    Slide 86 text

    86 Global state Initial global state Props View Props View Redux Component Component mapStateToProps

    Slide 87

    Slide 87 text

    87 Global state Initial global state Dispatcher Props View Props View Redux Component Component mapStateToProps dispatch

    Slide 88

    Slide 88 text

    88 Global state Initial global state Dispatcher Props View Props View Redux Component Component mapStateToProps dispatch Reducers

    Slide 89

    Slide 89 text

    89 5. Example

    Slide 90

    Slide 90 text

    90 SymfonyInsight Portfolio

    Slide 91

    Slide 91 text

    91

    Slide 92

    Slide 92 text

    92 Global state { dateInterval: string, isLoading: boolean, projects: Project[] | null, lastUpdated: Date | null, }

    Slide 93

    Slide 93 text

    93

    Slide 94

    Slide 94 text

    94

    Slide 95

    Slide 95 text

    95

    Slide 96

    Slide 96 text

    96 function mapStateToProps(globalState) { return { dateInterval: state.dateInterval, projects: state.projects, lastUpdated: state.lastUpdated, }; } class SidebarView extends Component { // ... } export const Sidebar = connect(mapStateToProps)(SidebarView);

    Slide 97

    Slide 97 text

    97 function mapStateToProps(globalState) { return { dateInterval: state.dateInterval, projects: state.projects, lastUpdated: state.lastUpdated, }; } class SidebarView extends Component { // ... } export const Sidebar = connect(mapStateToProps)(SidebarView); Redux

    Slide 98

    Slide 98 text

    class SidebarView extends Component { // ... render() { return (
    this.updateDateInterval(i)} />
    • All projects
    • {this.props.projects.map( project => )}
    Data last updated {moment(this.props.lastUpdated).fromNow()}
    ); 98

    Slide 99

    Slide 99 text

    99 class SidebarView extends Component { updateDateInterval(interval) { this.props.dispatch({ type: 'SET_DATE_INTERVAL', dateInterval: interval }); } // ... }

    Slide 100

    Slide 100 text

    100 class SidebarView extends Component { updateDateInterval(interval) { this.props.dispatch({ type: 'SET_DATE_INTERVAL', dateInterval: interval }); } // ... } Redux

    Slide 101

    Slide 101 text

    101 import {storage} from '../storage/storage'; export const dateIntervalReducer = (state, action) => { if (typeof state === 'undefined') { return storage.get('portfolio-date-interval', 2); } switch (action.type) { case 'SET_DATE_INTERVAL': storage.set('portfolio-date-interval', action.dateInterval); return action.dateInterval; default: return state; } };

    Slide 102

    Slide 102 text

    102 import {storage} from '../storage/storage'; export const dateIntervalReducer = (state, action) => { if (typeof state === 'undefined') { return storage.get('portfolio-date-interval', 2); } switch (action.type) { case 'SET_DATE_INTERVAL': storage.set('portfolio-date-interval', action.dateInterval); return action.dateInterval; default: return state; } }; Current state

    Slide 103

    Slide 103 text

    103 import {storage} from '../storage/storage'; export const dateIntervalReducer = (state, action) => { if (typeof state === 'undefined') { return storage.get('portfolio-date-interval', 2); } switch (action.type) { case 'SET_DATE_INTERVAL': storage.set('portfolio-date-interval', action.dateInterval); return action.dateInterval; default: return state; } }; Current state Initial state

    Slide 104

    Slide 104 text

    104 import {storage} from '../storage/storage'; export const dateIntervalReducer = (state, action) => { if (typeof state === 'undefined') { return storage.get('portfolio-date-interval', 2); } switch (action.type) { case 'SET_DATE_INTERVAL': storage.set('portfolio-date-interval', action.dateInterval); return action.dateInterval; default: return state; } }; Current state Initial state New state

    Slide 105

    Slide 105 text

    105 import {combineReducers} from 'redux'; export const rootReducer = combineReducers({ dateInterval: dateIntervalReducer, isLoading: isLoadingReducer, projects: projectsReducer, lastUpdated: lastUpdatedReducer, // ... });

    Slide 106

    Slide 106 text

    106 Conclusion

    Slide 107

    Slide 107 text

    Thanks! 107 For any question: ▪ @titouangalopin on Twitter ▪ titouan.galopin @symfony.com