Upgrade to Pro — share decks privately, control downloads, hide ads and more …

React Patterns

Chris Pearce
November 02, 2017

React Patterns

An educational exploration and workshop of React fundamentals and patterns.

Chris Pearce

November 02, 2017
Tweet

More Decks by Chris Pearce

Other Decks in Programming

Transcript

  1. 2 Table of Contents 01 02 03 04 JSX Elements

    Components Props & Children Undirectional data flow Back to Basics No Fuss Composition Higher-order-Component Function-as-Child / Render Prop Implicit-Child-Control / Child Mapping Component Injection Element Injection Patterns Exercise
  2. 6 eclarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative

    Declarative eclarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative eclarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative eclarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative eclarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative eclarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative eclarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative eclarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative eclarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative eclarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative eclarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative eclarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative eclarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative eclarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative eclarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative eclarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative eclarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative eclarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative eclarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative eclarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative eclarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative eclarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative eclarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative eclarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative eclarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative Declarative “ Elements describe your UI tree ” – Somebody Else, Somewhere else
  3. 9 “ They’re the blocks that opt you into a

    better data flow model ” – Another Person, Unknown
  4. 12 “ Props are how you pass data down your

    application. They’re like function arguments! ” – Another Person, Unknown
  5. 13 “ Children are a special prop and complete our

    tree structure ” – Another Person, Unknown
  6. 14 “ ANY value can be passed around within props.

    It’s just JavaScript! ” – Another Person, Unknown
  7. 16 “ Data flows down and up in a circular

    fashion and always maintains a singular source of truth ” – Another Person, Unknown
  8. 22 function withValueHOC(Component) { return function WrapComponent(props) { return <Component

    value={42} {...props} />; }; } const NewComponent = hoc(MyComponent); What does it look like?
  9. 23 Common Use Cases • Connecting or providing external state

    sources - createContainer, withForm, withQuery • Abstracting common prop mapping - mapProps
  10. 24 • Configurable at declaration time. • Easily composable -

    just functions! • Output is less obvious than other patterns. • Easily overdone - too many small pieces. • Merging props can be a problem. • If HoC output props dont match desired expected input, mapping is cumbersome. Pros & Cons
  11. 25 The vast majority of HOCs should just be enhancing

    props and managing side effects, not changing UI hierarchy. Quick Mention
  12. 29 function WrapComponent(props) { return props.children({value: 42}) } function NewComponent()

    { return ( <WrapComponent> {({value}) => <MyComponent value={value}>} </WrapComponent> ) } What does it look like?
  13. 30 Common Use Cases • Mostly interchangeable with HoCs •

    Highly abstracted components with multiple delegated rendering concerns - Calendar
  14. 31 • Configurable when creating via props. • Explicit data

    mapping. • In flow of data not always where you want. • Large composition chains become verbose. • Simpler mapping to desired props Pros & Cons
  15. 32 https://www.npmjs.com/package/react-media See how this component lets you pass a

    function which will be called to render something given some parameters. Example
  16. 35 function WrapComponent(props) { return React.cloneElement(props.children, {value: 42}); } function

    NewComponent() { return ( <WrapComponent> <MyComponent /> </WrapComponent> ) } What does it look like?
  17. 36 Common Use Cases • Abstractions providing “nicer” APIs with

    less moving pieces (and less indirection) - Field, SidebarNavigation
  18. 38 Quick Note You don’t have to map just using

    “clone”. You could map to totally different components using the input as a declarative configuration.
  19. 41 function WrapComponent(props) { const Render = props.Render; return <Render

    value={42} /> } function NewComponent() { return ( <WrapComponent Render={MyComponent} /> ) } What does it look like?
  20. 43 • Clean, optimal separation of abstractions. • Not the

    clearest UI hierarchy to read. • Implicit flow of data. • Slightly less worry about breaking purity Pros & Cons
  21. 46 function WrapComponent(props) { return <div>{props.icon} Text</div> } function NewComponent()

    { return ( <WrapComponent icon={<Icon />} /> ) } What does it look like?
  22. 47 Common Use Cases • Where an abstracted component has

    multiple parts to render (just children is not enough) - MenuItem
  23. 48 • Simple and explicit • Has to be mixed

    with child-mapping to get extra data Pros & Cons
  24. 51 Considering our components “pure” allows us to optimise updates

    and avoid wasteful rendering. If the input values have not changed we need not try updating.