Code Monkey home page Code Monkey logo

react-hook-hooked's Introduction

react-hook-hooked Build Status codecov npm

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: onCallback(() => {
    // do something
    console.log(title);
  }, [title]),
});

const MyComponent = ({ title, handleButtonPress }) => (
  <button onClick={handleButtonPress}>{title}</button>
);

export default hooked(myHook)(MyComponent);

If you have a hook that receives arguments

const myHook = (title) => ({
  title,
  handleButtonPress: onCallback(() => {
    // do something
    console.log(title);
  }, [title]),
});

const MyComponent = ({ title, handleButtonPress }) => (
  <button onClick={handleButtonPress}>{title}</button>
);

export default hooked(myHook, ({ name }) => name)(MyComponent);

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 title='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:

type Props = { title: string };

// This is an union with Props, since it returns the received `title` as an attribute
type Hooked = Props & { handleButtonPress: () => void };

const myHook = (title: string): Hooked => ({
  title,
  handleButtonPress: onCallback(() => {
    // do something
    console.log(title);
  }, [title]),
});

const MyComponent = ({ title, handleButtonPress }: Hooked) => (
  <button onClick={handleButtonPress}>{title}</button>
);

export default hooked<Props, Hooked>(myHook, ({ name }: Props) => name)(MyComponent);

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.

react-hook-hooked's People

Contributors

dependabot[bot] avatar fjcaetano avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.