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
22 function withValueHOC(Component) { return function WrapComponent(props) { return ; }; } 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
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
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
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
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
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.