react-hook-hooked
A nifty little HOC to add hooks to your React components.
Installation
We recommend installing using yarn
:
$ yarn add react-hook-hooked
Or, if you prefer npm
:
$ npm -S react-hook-hooked
Usage
hooked
is a high order component that receives as arguments the hook to be attached to the
component and an optional function to extract the hook's arguments from the component's props.
A simple hook that receives no arguments
const myHook = handleButtonPress: ; const MyComponent = title handleButtonPress <button =>title</button>; myHookMyComponent;
If you have a hook that receives arguments
const myHook = title title handleButtonPress: ; const MyComponent = title handleButtonPress <button =>title</button>; myHook name nameMyComponent;
When you use MyComponent and set its props, the function passed as the second argument to hooked
will extract the hook's args from the component's props:
<MyComponent ='foobar' />
Caveat
If you're using Typescript ❤️ in your project, your component will have to receive a union of the component's props and the hook's return type:
; // This is an union with Props, since it returns the received `title` as an attribute; ; ; ;
This way, externally your MyComponent will expect only title
as its props. By default, hooked
's
extractor is a passthru function that sends the received props to the hook.