introduction to react design patterns
play

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


  1. Introduction to React Design Patterns Brandon Williams Amazee Labs

  2. Before We Begin

  3. React Design Patterns Design patterns are formalized best practices that the Declarative programmer can use to solve common problems Flexible when designing an application or system. Library But these are just my opinions .

  4. Brandon Williams Lead Developer Amazee Labs Drupal 8 sites since Alpha React projects since 2016 SPAs, Interactive Touch Displays, Embeddable Widgets, Improved Filtered Search GraphQL Module image

  5. Design Patterns

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

  7. Presentational/Container Components Presentational Container class CommentListContainer extends const Commentlist = comments => ( React.Component { <ul> componentDidMount() { {comments.map( loadComments("/my-comments.json") ({ body, author }) => } <li>{body}-{author}</li> render() { )} return <CommentList </ul> comments={this.state.comments} ) />; } }

  8. Presentational/Container Components Why? Difficulty Separation of concerns Reusable Testable Style guides

  9. Stateless Components + Functional Programming Pure functions const SubmitButton = ({ label, No side-effects or shared state onSubmit, Declarative disabled, Composition over inheritance }) => ( <input Immutable state type="submit" disabled={disabled} value={label} onClick={onSubmit} /> );

  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

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

  12. Higher Order Components/Functions const withDisabledState = withState( const SubmitButton = ({ 'disabled', label, 'setDisabled', onSubmit, false); disabled, }) => (<input const withDisabledOnSubmit = type="submit" withHandlers({ disabled={disabled} onSubmit: ({ setDisabled }) => value={label} () => setDisabled(true), onClick={onSubmit} }) />); const ButtonWithDisable = compose( withDisabledState, withDisabledOnSubmit, )(<SubmitButton label="submit" />)

  13. Higher Order Components/Functions Why? Difficulty Keep code DRY Keep functional components pure Separation of concerns

  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" /> )} />

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

  16. CFP Open! 2018.texascamp.org

  17. Join us for contribution sprints Friday, April 13, 2018 Mentored First time General Core sprint sprinter workshop sprint 9:00-18:00 9:00-12:00 9:00-18:00 Room: 103 Room: 101 Room: 104 #drupalsprint

  18. Questions? Brandon Williams @rocketeerbkw

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend