|
| 1 | +# hoc |
| 2 | + |
| 3 | +hoc provide a simple apis to create javascript reusable components. It provide an abstraction from underlying framework, helps reuse the same component for different libraries with minimum code changes. |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +# features! |
| 8 | + - Promotes vanilla javascript use |
| 9 | + |
| 10 | + |
| 11 | +```js |
| 12 | +// ExampleComponent.js |
| 13 | + |
| 14 | +import { component } from 'hoc'; |
| 15 | +import afterRender from './hooks/afterRender'; |
| 16 | +import afterUpdate from './hooks/afterUpdate'; |
| 17 | +import beforeRender from './hooks/beforeRender'; |
| 18 | +import beforeUpdate from './hooks/beforeUpdate'; |
| 19 | +import beforeUnmount from './hooks/beforeUnmount'; |
| 20 | +import onClickHanlder from './handlers/onClickHanlder'; |
| 21 | +import onScrollHanlder from './handlers/onScrollHanlder'; |
| 22 | +import ExampleComponentTmpl from './templates/ExampleComponentTmpl'; |
| 23 | + |
| 24 | +const state = { |
| 25 | + salutation: 'Dear' |
| 26 | +}; |
| 27 | + |
| 28 | +const instanceProps = { |
| 29 | + counter: 0 |
| 30 | +}; |
| 31 | + |
| 32 | +@component({ |
| 33 | + state, |
| 34 | + afterRender, |
| 35 | + afterUpdate, |
| 36 | + beforeRender, |
| 37 | + beforeUpdate, |
| 38 | + beforeUnmount, |
| 39 | + instanceProp, |
| 40 | + eventHandler: { |
| 41 | + onClickHanlder, |
| 42 | + onScrollHanlder |
| 43 | + }, |
| 44 | + template: ExampleComponentTmpl |
| 45 | +}) |
| 46 | +export default class ExampleComponent {} |
| 47 | +``` |
| 48 | +```js |
| 49 | +// ExampleComponentTmpl.js |
| 50 | +// This is preact functional component |
| 51 | + |
| 52 | +import {h} from 'preact'; |
| 53 | + |
| 54 | +const ExampleComponentTmpl = (props) => { |
| 55 | + const { state, onClickHander } = props; |
| 56 | + const {salutation} = state; |
| 57 | + return( |
| 58 | + <div> |
| 59 | + <p> {salutation} customer! </p> |
| 60 | + <button onClick={onClickHander}>Say Hello </button> |
| 61 | + </div> |
| 62 | + ); |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +### state |
| 67 | +**state** is component local state. It can be modified with `setState` function. Every call to `setState` will cause component re-rending. In case you do not want render the component every time state changes, please use instanceProp. |
| 68 | + |
| 69 | +### instanceProp |
| 70 | +**instanceProp** is same as *state* but it offers its own getter `getInstanceProp` and setter `setInstanceProp`. instanceProp changes do not call `render`. |
| 71 | + |
| 72 | +### hooks |
| 73 | +hoc offers multiple lifecycle hooks get attached to underlying library lifecycle methods. |
| 74 | + - **beforeRender** gets called before component's render method |
| 75 | + - **afterRender** gets called after component is mounted |
| 76 | + - **beforeUpdate** gets called before components is updated |
| 77 | + - **afterUpdate** gets called after componet is updated |
| 78 | + - **beforeUnmount** gets called before component is removed from current DOM |
| 79 | + |
| 80 | +### eventHandler |
| 81 | +**eventHandler** is a collection of dom event handlers i.e. click, scroll, mouseover etc. Event handlers are plain javascript functions and surely not tightely coupued with any underlying library. |
| 82 | +```js |
| 83 | +const onClickHandler = (props, e) => { |
| 84 | + e.preventDefault(); |
| 85 | + const { state, setState, getInstanceProp } = props; |
| 86 | + return setState({ |
| 87 | + ...state, |
| 88 | + updateMsg: 'I am updated' |
| 89 | + }); |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +### template |
| 94 | +**template** represents presentation layer. Mostly it's html is going to rendered in the browser. |
| 95 | + |
| 96 | + |
0 commit comments