Giter Site home page Giter Site logo

hocify's Introduction

HOCify · Build Status Coverage Status

HOCify (H-oh-see-ify) is a simple library that converts hooks to HOCs for compatibility with class-based components.

Hooks are great! They're the React team's answer to many problems in React today. However, using them comes with a prerequisite:

Hooks can only be called inside the body of a function component.

This is unfortunate because it prevents us from using newer hook-based modules in our older class-based components.

This library aims to soften that prerequisite by giving you a reusable tool to convert some hooks into higher-order components.

Disclaimer: The purpose of "using hooks" within class components is more for compatibility of newer hook-based modules with older class-based components. If your component is already implemented as a function, then use the hook directly. If you're writing a new component, try writing it as a function component.

Installation

npm install --save hocify

Usage

hocify is a function that takes in a custom hook and returns an HOC.

⚠️️ There are a few things to note ️️️️️️⚠️

  1. The function you feed into hocify is a hook and thus must follow the rules of hooks
  2. The arguments to this hook are the props of the wrapped component. You can write a hook inline to hocify that uses these props as an input to other hooks.
  3. The resulting inline hook must return an object OR null. This object will be spread onto the input component as props.

ExampleComponent.js

import React from 'react';
import hocify from 'hocify';
import useMyCustomHook from './useMyCustomHook';

// 1) this function must follow the rules of hooks
// 2) `props` are the outer props of this function
const withMyCustomHook = hocify(props => {
  const result = useMyCustomHook(props.inputValue);

  // 3) the resulting hook _must_ return an object OR `null`.
  return { data: result };
});

class ExampleComponent extends React.Component {
  render() {
    const { data } = this.props;
  }
}

export default withMyCustomHook(ExampleComponent);

ParentComponent.js

import React from 'react';
import ExampleComponent from './ExampleComponent';

function ParentComponent() {
  // these props are the arguments to the inline hook in the `hocify` call above
  //                        👇👇👇
  return <ExampleComponent inputValue="test" anotherProp={5} />;
}

export default ParentComponent;

Examples

Using two or more hooks with hocify

The following example shows how you can use two hooks with hocify. Note that it's better to create a combined custom hook over creatitng multiple HOCs.

import React from 'react';
import hocify from 'hocify';
import useHookOne from './useHookOne';
import useHookTwo from './useHookTwo';

const withHooks = hocify(() => {
  const one = useHookOne();
  const two = useHookTwo();
  
  return { one, two };
});

class ClassComponent extends React.Component {
  // ... 
}

export default withHooks(ClassComponent);

Reacting to prop changes

The following example shows how you can use props in hocify(props => to react to prop changes. There is a useEffect in our example hook that will re-run if the id changes.

useFetchMovie.js

function useFetchMovie(id) {
  const [movie, setMovie] = useState(null);
  
  useEffect(() => {
    async function getData() {
      const response = await fetch(`/api/movies/${id}`);
      const movie = await response.json();
      setMovie(movie);
    }
    
    getData();
  }, [id]);
  
  return movie;
}

MyComponent.js

import React, { useState } from 'react';
import useFetchMovie from './useFetchMovie';

const withFetchMovie = hocify(props => {
  const movie = useFetchMovie(props.id);
  return { movie };
});

class MyComponent extends React.Component {
  render() {
    const { movie } = this.props;
    
    // ...
  }
}

export default withFetchMovie(MyComponent);

hocify's People

Contributors

ricokahler 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.