Slide 1

Slide 1 text

IN REACT HIGHER ORDER COMPONENTS

Slide 2

Slide 2 text

✓ @CEF62 • GITHUB/CEF62 ✓ SENIOR ENGINEER @ ✓ ITALIAN REACT & ANGULAR COMMUNITIES

Slide 3

Slide 3 text

FROM DOCUMENTS TO COMPONENTS

Slide 4

Slide 4 text

HYPERTEXT MARKUP LANGUAGE

Title Text

Some text about something

Slide 5

Slide 5 text

AJAX & DOM MANIPULATION

$(document).ready(() => { $.ajax(‘my-url.php’) .done((data) => { $(‘#my-title’).text(data.title) $(‘#my-contents’).text(data.contents) }) })

Slide 6

Slide 6 text

ANGULARJS
angular.module(‘app’, []) .directive(‘myPage’, function($http) { return { scope: { myUrl: ‘@’} template: `<div> <h1>{{myTitle}}</h1> <p>{{myContents}}</p> </div>` link: function(scope) { $http.get(scope.myUrl).success((data) => { scope.myTitle = data.title scope.myContents = data.contents }) } } })

Slide 7

Slide 7 text

REACT
class App extends React.Component { componentWillMount() { axios.get(this.props.myUrl) .then(({ title, contents }) => { this.setState({ title, contents }) }) } render() { return ( <div> <h1>{this.state.title}</h1> <p>{this.state.contents}</p> </div> ) } } ReactDOM.render( <App myUrl=“my-url.php” />, document.getElementById(‘root’) )

Slide 8

Slide 8 text

ALL MODERN FRAMEWORKS ARE COMPONENT ORIENTED •REACT •ANGULAR 2 •POLYMER

Slide 9

Slide 9 text

AN APPLICATION IS A TREE OF COMPONENTS EVERYTHING IS A COMPONENT

Slide 10

Slide 10 text

COMPONENTS ARE • Composable • Encapsulated • Reusable • Easy to design

Slide 11

Slide 11 text

SMALL, REUSABLE, PURE FUNCTIONS, EASY TO TEST PRESENTATIONAL COMPONENTS

Slide 12

Slide 12 text

STATELESS FUNCTIONAL COMPONENTS const Label = (props) =>

{props.children}

const Icon = (props) => { return ( {props.label} ) } const App = () =>
Some label

Slide 13

Slide 13 text

COMPONENT LIFECYCLE

Slide 14

Slide 14 text

COMPONENT LIFECYCLE class MyComponent extends React.Component { // Component Initialization constructor(props) {} // component ready to be mounted componentWillMount() {} // Props have changed componentWillReceiveProps(nextProps) {} // prevent rendering if not required shouldComponentUpdate(nextProps, nextState) {} // Prepare before rendering componentWillUpdate(nextProps, nextState) {} }

Slide 15

Slide 15 text

COMPONENT LIFECYCLE class MyComponent extends React.Component { // create/update the component render() {} // Component DOM it’s been created componentDidMount() {} // Component DOM it’s been updated componentDidUpdate(prevProps, prevState) {} // cleanup before component destruction componentWillUnmount() {} }

Slide 16

Slide 16 text

CONTEXT- AWARE, STATEFUL, DYNAMIC, POWERFUL CONTAINER COMPONENTS

Slide 17

Slide 17 text

STATEFUL COMPONENTS class Toggle extends React.Component { constructor(props) { super(props) this.state = { enabled: false } this.toggle = this.toggle.bind(this) } toggle() { this.setState({ enabled: !this.state.enabled }) } render() { const cls = this.state.enabled ?'btn-success':'btn-danger' return ( {this.props.label} ) } }

Slide 18

Slide 18 text

REDUCE CODE BOILERPLATE

Slide 19

Slide 19 text

COMPONENTS SHARE FUNCTIONALITIES class Box extends React.Component { constructor(props) { this.state = { info: {} } } componentWillMount() { axios.get(this.props.url).then( ({ data }) => this.setState({ info: data }) ) } render() { return
{this.state.info.text}
} } class User extends React.Component { constructor(props) { this.state = { user: {} } } componentWillMount() { axios.get(this.props.url).then( ({ data }) => this.setState({ user: data }) ) } render() { return
{this.state.user.name}
} }

Slide 20

Slide 20 text

REACT MIXINS

Slide 21

Slide 21 text

MIXINS const LoadDataMixin = { loadData(url, targetField) { axios.get(url).then( ({ data }) => this.setState({ [targetField]: data }) ) } }

Slide 22

Slide 22 text

MIXINS const BoxInfo = React.createClass({ mixins: [LoadDataMixin], getInitialState() { return { info: {} } }, componentWillMount() { this.loadData(this.props.url, 'info') }, render() { return
{this.state.info.text}
} }) const UserPanel = React.createClass({ mixins: [LoadDataMixin], getInitialState() { return { user: {} } }, componentWillMount() { this.loadData(this.props.url, 'user') }, render() { return
{this.state.user.name}
} })

Slide 23

Slide 23 text

HIGHER ORDER FUNCTIONS

Slide 24

Slide 24 text

— Marijn Haverbeke (eloquent javascript) HIGHER-ORDER FUNCTIONS ALLOW US TO ABSTRACT OVER ACTIONS, NOT JUST VALUES. ” “

Slide 25

Slide 25 text

HOF function applyVat(vat) { return (value) => ((vat + 100) / 100) * value } const applyVat22 = applyVat(22) console.log(applyVat22(100)) // print -> 122 function maybe(condition, action) { if (condition) { action() } } const action = (msg) => () => console.log(msg) maybe(true, action(`It works!`)) maybe(false, action(`Doesn't works!`)) // print -> It works!

Slide 26

Slide 26 text

(TO)HIGHER ORDER COMPONE NTS FROM HIGHER ORDER FUNCTIONS

Slide 27

Slide 27 text

const Wrapped = () =>

Wrapped Comp

const wrapper = (Comp) => () => { return
} const FinalComponent = wrapper(Wrapped) const App = () => { return (
) }

Slide 28

Slide 28 text

REDUCE DUPLICATION OF CODE const loadData = (component) => { return class LoadDataWrapper extends React.Component { constructor(props) { this.state = { data: {} } } componentWillMount() { axios.get(this.props.url).then( ({ data }) => this.setState({ data }) ) } render() { const props = Object.assign({}, this.props, this.state) return React.createElement(component, props) } } } const Box = loadData((props) =>
{props.data.text}
) const User = loadData((props) =>
{props.data.name}
)

Slide 29

Slide 29 text

DECORATOR PROPOSAL @loadData class Box extends React.Component { render() { return
{this.props.data.text}
} } @loadData class User extends React.Component { render() { return
{this.props.data.name}
} }

Slide 30

Slide 30 text

- Declarative - Customizable - Easy to read - Enforce composition HOC VS MIXINS Imperative - Favors inheritance - Hard to read - Method names collision - Access component state -

Slide 31

Slide 31 text

BEST PRACTICES HIGHER ORDER COMPONENTS • Expose wrapped component • Pass props to wrapped component • Expose statics methods and props from wrapped component

Slide 32

Slide 32 text

CODE SEE ONLINE

Slide 33

Slide 33 text

THANKS! @CEF62