Introduction to React Design Patterns Brandon Williams Amazee Labs - - PowerPoint PPT Presentation

introduction to react design patterns
SMART_READER_LITE
LIVE PREVIEW

Introduction to React Design Patterns Brandon Williams Amazee Labs - - PowerPoint PPT Presentation

Introduction to React Design Patterns Brandon Williams Amazee Labs Before We Begin React Design Patterns Design patterns are formalized best practices that the Declarative programmer can use to solve common problems Flexible when designing


slide-1
SLIDE 1

Brandon Williams Amazee Labs

Introduction to React Design Patterns

slide-2
SLIDE 2

Before We Begin

slide-3
SLIDE 3

React

Declarative Flexible Library

Design Patterns

Design patterns are formalized best practices that the programmer can use to solve common problems when designing an application or system.

But these are just my opinions.

slide-4
SLIDE 4

Brandon Williams Lead Developer

Amazee Labs

image

Drupal 8 sites since Alpha React projects since 2016 SPAs, Interactive Touch Displays, Embeddable Widgets, Improved Filtered Search GraphQL Module

slide-5
SLIDE 5

Design Patterns

slide-6
SLIDE 6

Presentational/Container Components

Presentational Container

How things look Little to no state Receive all data via props Functional component Same use case as Twig templates How things work Render presentational and container components Provide data and behavior Stateful

slide-7
SLIDE 7

Presentational/Container Components

Presentational Container

const Commentlist = comments => ( <ul> {comments.map( ({ body, author }) => <li>{body}-{author}</li> )} </ul> )

class CommentListContainer extends React.Component { componentDidMount() { loadComments("/my-comments.json") } render() { return <CommentList comments={this.state.comments} />; } }

slide-8
SLIDE 8

Presentational/Container Components

Why? Difficulty

Separation of concerns Reusable Testable Style guides

slide-9
SLIDE 9

Stateless Components + Functional Programming

Pure functions No side-effects or shared state Declarative Composition over inheritance Immutable state const SubmitButton = ({ label,

  • nSubmit,

disabled, }) => ( <input type="submit" disabled={disabled} value={label}

  • nClick={onSubmit}

/> );

slide-10
SLIDE 10

Stateless Components + Functional Programming

Why? Difficulty

Declarative programming for declarative library Immutability helps avoid render errors Easier to reason about component dependencies Fun vocabulary: map, reduce, functor, monad, currying

slide-11
SLIDE 11

Higher Order Components/Functions

A function that returns a component/function Wraps functional components Recompose library for React integration Same use case as PHP Traits or OO Decorator pattern const withHideLoading = (BaseComponent) => (props) => ( {props.loading && <BaseComponent />} ) const ButtonWithLoading = withHideLoading(<SubmitButton label="Submit" />) <ButtonWithLoading loading={true} />

slide-12
SLIDE 12

Higher Order Components/Functions

const withDisabledState = withState( 'disabled', 'setDisabled', false); const withDisabledOnSubmit = withHandlers({

  • nSubmit: ({ setDisabled }) =>

() => setDisabled(true), }) const SubmitButton = ({ label,

  • nSubmit,

disabled, }) => (<input type="submit" disabled={disabled} value={label}

  • nClick={onSubmit}

/>); const ButtonWithDisable = compose( withDisabledState, withDisabledOnSubmit, )(<SubmitButton label="submit" />)

slide-13
SLIDE 13

Higher Order Components/Functions

Why? Difficulty

Keep code DRY Keep functional components pure Separation of concerns

slide-14
SLIDE 14

Render Props/Function as Children

Alternative to HoC Difficulty

const HideLoading = (props) => { if (props.loading) { return null; } return this.props.render(); } <HideLoading loading{true} render={() => ( <SubmitButton label="submit" /> )} />

slide-15
SLIDE 15

Flux

Difficulty

Pattern for managing data All state flows in one direction All state managed in the store State changes cause re-renders Redux

slide-16
SLIDE 16

CFP Open! 2018.texascamp.org

slide-17
SLIDE 17

Join us for contribution sprints

Friday, April 13, 2018

9:00-18:00 Room: 103

Mentored Core sprint First time sprinter workshop General sprint

#drupalsprint

9:00-12:00 Room: 101 9:00-18:00 Room: 104

slide-18
SLIDE 18

Questions? Brandon Williams

@rocketeerbkw