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

ARP-real-world.pdf

 ARP-real-world.pdf

Component patterns have been around for a while, but in this
talk, I'll explain the ins and outs of each pattern in relatable language.

To do this, I'll employ graphical illustrations and everyday analogies
that'll help you intuitively understand these patterns.
The talk will then come full circle as I point you real-world examples of
these patterns in use!

Avatar for Ohans Emmanuel

Ohans Emmanuel

April 24, 2020
Tweet

More Decks by Ohans Emmanuel

Other Decks in Programming

Transcript

  1. • Snr. Frontend Engineer, Omio • ✏ Writer & Published

    Author • I dance & listen to Music while Quarantining alone. This is me.
  2. • Introduce each pattern • ✏ See a basic example

    • Explore open-source community examples • ⁉ Answer questions How the talk is Structured
  3. Custom Hooks: The foundational Pattern A mechanism to reuse stateful

    logic const useAdvancedPatterns = () => { } // state and effects isolated here // Must be called from a React fn component/other custom hook useAdvancedPatterns()
  4. Small set of functions that do one thing and help

    you use the logic embedded in them. useEffectOnce() // a modified useEffect hook that only runs once. // subscribe to events. useEvent() usePromise() // resolves promise only while component is mounted
  5. The Compound Components Pattern Communicate the relationship between UI components

    and share implicit state by leveraging an explicit parent-child relationship. <MediumClap> <MediumClap.Icon /> <MediumClap.Total /> <MediumClap.Count /> </MediumClap>
  6. <MediumClap> <Icon /> <Total /> <Count /> </MediumClap> This is

    equally valid: UI state is managed by the Parent and communicated through the component tree via the Context API
  7. Consider how an Accordion component from ReachUI is used: import

    { Accordion, AccordionItem, AccordionButton } from “@reach/accordion”; return ( <Accordion> <AccordionItem> <h3><AccordionButton> Header </AccordionButton></h3> <AccordionPanel> Message </AccordionPanel> </AccordionItem> </Accordion> )
  8. Pros 1. Flexible Markup Structure 2. Reduced Complexity 3. Separation

    of concerns 1. A little more work needs to be done Cons
  9. Extensible Styles Overriding and adding new styles Core principle: Allow

    users style your components like they would other elements/ components <YourComponent className=`shouldWork`/> <YourComponent style=`shouldWork`/>
  10. The Control Props Pattern Control props allow users of your

    component to control the UI state via certain “control” props. <YourComponent value=`string` onChange= {fn}/>
  11. <Rating name=`size-medium` defaultValue={2} /> Consider the Rating component from Material-UI.

    // controlled <Rating value={someValue} onChange={someCallback} name=`size-medium` defaultValue={2} />
  12. The Props Getters Pattern Provide a collection of props to

    users of your hooks/component const {getPropsCollection} = useYourHook() const propsCollection = getPropsCollection({ }) onClick: myClickHandler data-testId: `my-test-id`
  13. useTable({data}) To make composing UIs easier, it offers a set

    of prop getters. <table {...getTableProps()}> //other UI elements go here </table> const {getTableProps, getTableBodyProps} =
  14. State Initialisers Pattern Allowing for configurable initial state, and an

    optional state reset handler. const {value, reset} = useYourHook(initialState) ⁉ Passing props to state is generally frowned upon // internally const [internalState] = useState(initialState)
  15. import { Formik } from ‘formik’ Consider the contrived Formik

    usage below: <Formik initialValues={{ firstName: “”, lastName: “”, email: “” }}> </Formik>
  16. Pros 1. Important feature for most UI components 2. Easy

    to implement 1. May be trivial Cons
  17. State Reducers Pattern State reducers allow you to cede state

    control to the users of your component. Also, by leveraging action types, you minimise code duplicates on the user’s side. const values = useYourHook(reducer, …otherArgs)
  18. <Downshift stateReducer={stateReducer} /> Each time Downshift sets internal state, the

    reducer is invoked const stateReducer = (state, changes) => newState
  19. • Go use these examples • ✏ Let me know

    what you think. @OhansEmmanuel • ⛑ Stay safe! Thank you. https://bit.ly/react-patterns-real-world