Share
Embed
React Patterns
An educational exploration and workshop of React fundamentals and patterns.
Chris Pearce
November 02, 2017
Transcript
54 slides
-
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 -
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 -
9 “ They’re the blocks that opt you into a
better data flow model ” – Another Person, Unknown -
12 “ Props are how you pass data down your
application. They’re like function arguments! ” – Another Person, Unknown -
13 “ Children are a special prop and complete our
tree structure ” – Another Person, Unknown -
14 “ ANY value can be passed around within props.
It’s just JavaScript! ” – Another Person, Unknown -
16 “ Data flows down and up in a circular
fashion and always maintains a singular source of truth ” – Another Person, Unknown -
20 • Quick and Easy. • No Abstraction Overhead. •
Not reusable. Pros & Cons -
22 function withValueHOC(Component) { return function WrapComponent(props) { return <Component
value={42} {...props} />; }; } const NewComponent = hoc(MyComponent); What does it look like? -
23 Common Use Cases • Connecting or providing external state
sources - createContainer, withForm, withQuery • Abstracting common prop mapping - mapProps -
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 -
25 The vast majority of HOCs should just be enhancing
props and managing side effects, not changing UI hierarchy. Quick Mention -
26 https://github.com/kalohq/frontend/blob/develo p/src/core/decorators/with-query/with-query.js We use withQuery to pass in current
router location and validate & parse against a schema. Example -
29 function WrapComponent(props) { return props.children({value: 42}) } function NewComponent()
{ return ( <WrapComponent> {({value}) => <MyComponent value={value}>} </WrapComponent> ) } What does it look like? -
30 Common Use Cases • Mostly interchangeable with HoCs •
Highly abstracted components with multiple delegated rendering concerns - Calendar -
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 -
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 -
35 function WrapComponent(props) { return React.cloneElement(props.children, {value: 42}); } function
NewComponent() { return ( <WrapComponent> <MyComponent /> </WrapComponent> ) } What does it look like? -
36 Common Use Cases • Abstractions providing “nicer” APIs with
less moving pieces (and less indirection) - Field, SidebarNavigation -
37 • Provides less verbose API. • Very implicit, easy
to miss when reading. Pros & Cons -
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. -
39 https://github.com/kalohq/frontend/blob/deve lop/src/core/components/basic-form/basic-for m.js#L235-L266 See how we map from permissions
to disabled in our Field component. Example -
41 function WrapComponent(props) { const Render = props.Render; return <Render
value={42} /> } function NewComponent() { return ( <WrapComponent Render={MyComponent} /> ) } What does it look like? -
42 Common Use Cases • Interchangeable with FaCs • Overriding
root elements for semantics - Link, Box -
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 -
44 https://github.com/kalohq/ui/blob/master/src/co mponents/layout/layout.js#L25 We use the component prop on Box
to describe what is the root component to be rendered. Example -
46 function WrapComponent(props) { return <div>{props.icon} Text</div> } function NewComponent()
{ return ( <WrapComponent icon={<Icon />} /> ) } What does it look like? -
47 Common Use Cases • Where an abstracted component has
multiple parts to render (just children is not enough) - MenuItem -
48 • Simple and explicit • Has to be mixed
with child-mapping to get extra data Pros & Cons -
49 https://github.com/kalohq/frontend/blob/master/sr c/core/components/menu-item/menu-item.js#L118- L122 An “icon” can be passed as
an element to give full control over what is rendered in our menu items. Example -
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.