Slide 1

Slide 1 text

Life is a Slice of State React/Redux Patterns for a New Generation

Slide 2

Slide 2 text

Why Care About Design Patterns? ● Solid and consistent design patterns are crucial to writing maintainable, readable, and reusable code ● Speed up development time by providing well tested, and proven development paradigms ● They should solve challenges, and provide structure to common problems with regards to your codebase and/or team

Slide 3

Slide 3 text

I’m Ryan Haase Software Engineer @ Mavenlink I Love Design Patterns and Dogs GitHub: ryandhaase Twitter: @ryandhaase Email: [email protected]

Slide 4

Slide 4 text

Redux Data Flow ● Unidirectional data flow ● Consistent data lifecycle ● View -> Actions -> Reducers -> Store -> View Action Reducer Store View

Slide 5

Slide 5 text

Redux Actions ● Actions are plain old objects ● Send payloads of data from your application to your store ● Used to handle non-deterministic logic like async requests ● Event Emitters / Event Triggers

Slide 6

Slide 6 text

Reducers ● When dispatched, the actions indicate that something has happened but not how the state should change in response ● Reducers are pure functions that take the previous state and an action, and return the next state ● No side effects ● No mutations

Slide 7

Slide 7 text

Store ● An object containing the whole state of an application (State Tree) ● The state is read only, and can only be changed through dispatching actions

Slide 8

Slide 8 text

Selectors ● Sit between the store and view layer ● Act as “getters” that allow us to retrieve a slice from the state tree ● Derive Computations ● Single source of truth

Slide 9

Slide 9 text

Why Explore New Design Patterns? ● Consistent design patterns that are maintainable, readable, and reusable code ● Well tested, and proven development paradigms ● Solve challenges, and provide structure to common problems ● Maintainable ● Readable ● Reusable ● Testable ● Well Structured

Slide 10

Slide 10 text

Selectors: Pattern #1 ● Defining Selectors within Containers ○ Maintainable ✘ ○ Readable ✘ ○ Reusable ✘ ○ Testability ✘ ○ Well Structured ✘

Slide 11

Slide 11 text

Selectors: Pattern #2 ● Import Selectors from External Location ○ Maintainable ✘ ○ Readable ✔✘ ○ Reusable ✔ ○ Testability ✔ ○ Well Structured ✔✘

Slide 12

Slide 12 text

Selectors: Pattern #3 ● Colocating Selectors with Reducers ○ Maintainable ✔ ○ Readable ✔ ○ Reusable ✔✘ ○ Testability ✔ ○ Well Structured ✔

Slide 13

Slide 13 text

New Pattern: Model “Types” ● Define “getter” Selectors as a Model “Type” ● Colocate Computation Selectors ○ Maintainable ✔ ○ Readable ✔ ○ Reusable ✔✘ ○ Testability ✔ ○ Well Structured ✔

Slide 14

Slide 14 text

Model “Types”: Decorator ● Don’t Repeat Yourself! ● Extract away common selectors for accessing the state tree

Slide 15

Slide 15 text

Model “Types”: Dry it Up! ● Decorate Model “Types” ○ Maintainable ✔ ○ Readable ✔ ○ Reusable ✔ ○ Testability ✔ ○ Well Structured ✔

Slide 16

Slide 16 text

Model “Types”: PropTypes ● Don’t Repeat Yourself! ● Single source of truth for an object's prop type shape

Slide 17

Slide 17 text

Model “Types”: SpecExample ● Don’t Repeat Yourself! ● Factory for building spec examples throughout test suite

Slide 18

Slide 18 text

Model “Types”: CRUD Actions ● Don’t Repeat Yourself! ● Whether we are creating, reading, updating, or destroying, the only thing that changes, is the object we are working with….

Slide 19

Slide 19 text

Model “Types”: CRUD Actions ● Decorating our Model “Types” with our common CRUD Actions enforces: ○ Consistency ○ Readability ○ Maintainable ○ Reusability

Slide 20

Slide 20 text

Recap: Model “Types” Pattern ● Maintainable ✔ ○ Structural Design Pattern (Facade Pattern) ● Readable ● Reusable ● Testability ● Well Structured

Slide 21

Slide 21 text

Recap: Model “Types” Pattern ● Maintainable ✔ ● Readable ✔ ○ Focused Methods/Functions ○ Intention-Revealing Interfaces ● Reusable ● Testability ● Well Structured

Slide 22

Slide 22 text

Recap: Model “Types” Pattern ● Maintainable ✔ ● Readable ✔ ● Reusable ✔ ○ Selectors ○ Prop Types ○ CRUD Actions ○ Spec Examples - Factories ● Testability ● Well Structured

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

Thanks, I’m Ryan Haase Software Engineer @ Mavenlink I Love Design Patterns and Dogs GitHub: ryandhaase Twitter: @ryandhaase Email: [email protected]