0.1.0 • Published 7 years ago

guac-hoc v0.1.0

Weekly downloads
-
License
ISC
Repository
-
Last release
7 years ago

GUAC-HOC

WrappedComponent = higherOrderComponent1(higherOrderComponent2(WrappedComponent));
...
<WrappedComponent className='myClassName'/>

An example implementation is below. Notice the problem with className. Imagine that, as an end user, I define a component, apply the higher order component, then want to pass in a className prop. This higher order component would intercept it! Note that this is an issue with every prop, including event props such as onClick and onMouseDown. While it is possible to solve this with composition from the library manager's end, this can lead to ugly code and excessive boilerplate.

  function higherOrderComponent1(WrappedComponent) {  
    ...  
    return class WrapperComponent extends React.Component {  
      ...
      render() {
        return (
          /* className is intercepted and overridden, so some user-passed props 
          like className are not propagated to WrappedComponent*/
          <WrappedComponent {...this.props} className='styleClass1'/>
        );
      }
    }
  }

So instead, we let this library handle it.

  function higherOrderComponent(WrappedComponent) {
  ...  
    class WrapperComponent extends React.Component {
      constructor() {
        super();
        //Instead of following ES6 experimental structure, use this to have more java-like declarative syntax.
        this.bindAllMethods();
      }
      ...
      render() {
        return (
                                                     /* \/ Call this to retrieve the composed classNames.*/
          <WrappedComponent {...this.props} className={this.className()}/>
        );
      }
      //This line provides higher-order-component functionality to your components.
      return HOC(WrapperComponent);
    }
  }
npm install --save guac-hoc@latest
import HOC from 'guac-hoc/lib/HOC';
import Guac from 'guac-hoc/lib/Guac';
MethodParametersReturnsDescription
this.bindAllMethods()NoneNoneUse in constructor. Binds all class methods to the instance.
this.deleteUsedProps(propNames)propNames: list\<string>props: objectRemoves all props that you do not want exposed to your WrappedComponent.

These methods are special, reserved methods that each map to a prop. If your higher-order component intercepts or passes down any of these props to the WrappedComponent, instead implement the corresponding method and pass it or its return value down instead.

MethodParametersReturnsDescription
this.className()NonestringDefine the calculation for your higher-order component's className here and return it. Call this in render() to get the composed className and pass that value down.
this.onClick(event)eventNoneDefine onClick prop. Pass prop as a method reference as usual.
this.onMouseDown(event)eventNoneDefine onMouseDown prop. Pass prop as a method reference as usual.
this.onMouseUp(event)eventNoneDefine onMouseUp prop. Pass prop as a method reference as usual.
this.onMouseEnter(event)eventNoneDefine onMouseEnter prop. Pass prop as a method reference as usual.
this.onMouseLeave(event)eventNoneDefine onMouseLeave prop. Pass prop as a method reference as usual.
0.1.0

7 years ago

0.0.25

7 years ago

0.0.24

7 years ago

0.0.23

7 years ago

0.0.22

7 years ago

0.0.21

7 years ago

0.0.20

7 years ago

0.0.19

7 years ago

0.0.18

7 years ago

0.0.17

7 years ago

0.0.15

7 years ago

0.0.13

7 years ago

0.0.12

7 years ago

0.0.11

7 years ago

0.0.10

7 years ago

0.0.9

7 years ago

0.0.7

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago